mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-19 22:48:18 +00:00
Add angle<->direction conversion functions to vector math (Cleanup)
This commit is contained in:
parent
752a38ccfe
commit
7735557aa5
|
@ -62,17 +62,23 @@ inline T length(const vector2_base<T> &a)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
inline T distance(const vector2_base<T> a, const vector2_base<T> &b)
|
||||
inline T distance(const vector2_base<T> &a, const vector2_base<T> &b)
|
||||
{
|
||||
return length(a-b);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T dot(const vector2_base<T> a, const vector2_base<T> &b)
|
||||
inline T dot(const vector2_base<T> &a, const vector2_base<T> &b)
|
||||
{
|
||||
return a.x*b.x + a.y*b.y;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T angle(const vector2_base<T> &a)
|
||||
{
|
||||
return atan2f(a.y, a.x);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline vector2_base<T> normalize(const vector2_base<T> &v)
|
||||
{
|
||||
|
@ -80,6 +86,12 @@ inline vector2_base<T> normalize(const vector2_base<T> &v)
|
|||
return vector2_base<T>(v.x*l, v.y*l);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline vector2_base<T> direction(T angle)
|
||||
{
|
||||
return vector2_base<T>(cosf(angle), sinf(angle));
|
||||
}
|
||||
|
||||
typedef vector2_base<float> vec2;
|
||||
typedef vector2_base<bool> bvec2;
|
||||
typedef vector2_base<int> ivec2;
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include <game/generated/protocol.h>
|
||||
#include <game/generated/client_data.h>
|
||||
|
||||
#include <game/gamecore.h> // get_angle
|
||||
#include <game/client/ui.h>
|
||||
#include <game/client/render.h>
|
||||
#include "damageind.h"
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include <game/generated/protocol.h>
|
||||
#include <game/generated/client_data.h>
|
||||
|
||||
#include <game/gamecore.h> // get_angle
|
||||
#include <game/client/ui.h>
|
||||
#include <game/client/render.h>
|
||||
#include "emoticon.h"
|
||||
|
@ -114,7 +113,7 @@ void CEmoticon::OnRender()
|
|||
if (length(m_SelectorMouse) > 170.0f)
|
||||
m_SelectorMouse = normalize(m_SelectorMouse) * 170.0f;
|
||||
|
||||
float SelectedAngle = GetAngle(m_SelectorMouse) + 2*pi/24;
|
||||
float SelectedAngle = angle(m_SelectorMouse) + 2*pi/24;
|
||||
if (SelectedAngle < 0)
|
||||
SelectedAngle += 2*pi;
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include <game/generated/protocol.h>
|
||||
#include <game/generated/client_data.h>
|
||||
|
||||
#include <game/gamecore.h> // get_angle
|
||||
#include <game/client/gameclient.h>
|
||||
#include <game/client/ui.h>
|
||||
#include <game/client/render.h>
|
||||
|
@ -89,7 +88,7 @@ void CItems::RenderProjectile(const CNetObj_Projectile *pCurrent, int ItemID)
|
|||
m_pClient->m_pEffects->BulletTrail(Pos);
|
||||
|
||||
if(length(Vel) > 0.00001f)
|
||||
Graphics()->QuadsSetRotation(GetAngle(Vel));
|
||||
Graphics()->QuadsSetRotation(angle(Vel));
|
||||
else
|
||||
Graphics()->QuadsSetRotation(0);
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
#include <game/generated/client_data.h>
|
||||
#include <game/client/render.h>
|
||||
#include <game/gamecore.h>
|
||||
|
||||
#include "particles.h"
|
||||
|
||||
CParticles::CParticles()
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
#include <game/generated/protocol.h>
|
||||
#include <game/generated/client_data.h>
|
||||
|
||||
#include <game/gamecore.h> // get_angle
|
||||
#include <game/client/animstate.h>
|
||||
#include <game/client/gameclient.h>
|
||||
#include <game/client/ui.h>
|
||||
|
@ -30,7 +29,7 @@ void CPlayers::RenderHand(CTeeRenderInfo *pInfo, vec2 CenterPos, vec2 Dir, float
|
|||
//dir = normalize(hook_pos-pos);
|
||||
|
||||
vec2 HandPos = CenterPos + Dir;
|
||||
float Angle = GetAngle(Dir);
|
||||
float Angle = angle(Dir);
|
||||
if (Dir.x < 0)
|
||||
Angle -= AngleOffset;
|
||||
else
|
||||
|
@ -159,7 +158,7 @@ void CPlayers::RenderHook(
|
|||
float d = distance(Pos, HookPos);
|
||||
vec2 Dir = normalize(Pos-HookPos);
|
||||
|
||||
Graphics()->QuadsSetRotation(GetAngle(Dir)+pi);
|
||||
Graphics()->QuadsSetRotation(angle(Dir)+pi);
|
||||
|
||||
// render head
|
||||
RenderTools()->SelectSprite(SPRITE_HOOK_HEAD);
|
||||
|
@ -212,7 +211,7 @@ void CPlayers::RenderPlayer(
|
|||
if(m_pClient->m_LocalClientID == ClientID && Client()->State() != IClient::STATE_DEMOPLAYBACK)
|
||||
{
|
||||
// just use the direct input if it's local player we are rendering
|
||||
Angle = GetAngle(m_pClient->m_pControls->m_MousePos);
|
||||
Angle = angle(m_pClient->m_pControls->m_MousePos);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -253,7 +252,7 @@ void CPlayers::RenderPlayer(
|
|||
}
|
||||
}
|
||||
|
||||
vec2 Direction = GetDirection((int)(Angle*256.0f));
|
||||
vec2 Direction = vec2(direction(Angle));
|
||||
vec2 Position = mix(vec2(Prev.m_X, Prev.m_Y), vec2(Player.m_X, Player.m_Y), IntraTick);
|
||||
vec2 Vel = mix(vec2(Prev.m_VelX/256.0f, Prev.m_VelY/256.0f), vec2(Player.m_VelX/256.0f, Player.m_VelY/256.0f), IntraTick);
|
||||
|
||||
|
@ -379,8 +378,8 @@ void CPlayers::RenderPlayer(
|
|||
{
|
||||
vec2 Dir = vec2(pPlayerChar->m_X,pPlayerChar->m_Y) - vec2(pPrevChar->m_X, pPrevChar->m_Y);
|
||||
Dir = normalize(Dir);
|
||||
float HadOkenAngle = GetAngle(Dir);
|
||||
Graphics()->QuadsSetRotation(HadOkenAngle );
|
||||
float HadokenAngle = angle(Dir);
|
||||
Graphics()->QuadsSetRotation(HadokenAngle );
|
||||
//float offsety = -data->weapons[iw].muzzleoffsety;
|
||||
RenderTools()->SelectSprite(g_pData->m_Weapons.m_aId[iw].m_aSpriteMuzzles[IteX], 0);
|
||||
vec2 DirY(-Dir.y,Dir.x);
|
||||
|
|
|
@ -838,7 +838,7 @@ void CGameClient::ProcessEvents()
|
|||
if(Item.m_Type == NETEVENTTYPE_DAMAGEIND)
|
||||
{
|
||||
CNetEvent_DamageInd *ev = (CNetEvent_DamageInd *)pData;
|
||||
m_pEffects->DamageIndicator(vec2(ev->m_X, ev->m_Y), GetDirection(ev->m_Angle));
|
||||
m_pEffects->DamageIndicator(vec2(ev->m_X, ev->m_Y), vec2(direction(ev->m_Angle/256.0f)));
|
||||
}
|
||||
else if(Item.m_Type == NETEVENTTYPE_EXPLOSION)
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <engine/graphics.h>
|
||||
#include <engine/serverbrowser.h>
|
||||
#include <engine/storage.h>
|
||||
#include <game/gamecore.h>
|
||||
#include <game/gamecore.h> // StrToInts, IntsToStr
|
||||
#include "editor.h"
|
||||
|
||||
template<typename T>
|
||||
|
|
|
@ -47,28 +47,6 @@ public:
|
|||
bool Get(const char *pName, float *pValue);
|
||||
};
|
||||
|
||||
|
||||
inline vec2 GetDirection(int Angle)
|
||||
{
|
||||
float a = Angle/256.0f;
|
||||
return vec2(cosf(a), sinf(a));
|
||||
}
|
||||
|
||||
inline vec2 GetDir(float Angle)
|
||||
{
|
||||
return vec2(cosf(Angle), sinf(Angle));
|
||||
}
|
||||
|
||||
inline float GetAngle(vec2 Dir)
|
||||
{
|
||||
if(Dir.x == 0 && Dir.y == 0)
|
||||
return 0.0f;
|
||||
float a = atanf(Dir.y/Dir.x);
|
||||
if(Dir.x < 0)
|
||||
a = a+pi;
|
||||
return a;
|
||||
}
|
||||
|
||||
inline void StrToInts(int *pInts, int Num, const char *pStr)
|
||||
{
|
||||
int Index = 0;
|
||||
|
|
|
@ -360,7 +360,7 @@ void CCharacter::FireWeapon()
|
|||
for(int i = -ShotSpread; i <= ShotSpread; ++i)
|
||||
{
|
||||
float Spreading[] = {-0.185f, -0.070f, 0, 0.070f, 0.185f};
|
||||
float a = GetAngle(Direction);
|
||||
float a = angle(Direction);
|
||||
a += Spreading[i+2];
|
||||
float v = 1-(absolute(i)/(float)ShotSpread);
|
||||
float Speed = mix((float)GameServer()->Tuning()->m_ShotgunSpeeddiff, 1.0f, v);
|
||||
|
|
Loading…
Reference in a new issue