calculating with sin and cos

This topic is 3967 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hi everyone. i am working on a little game in my free time (no big project) i already have a simple colision detection and you can move around. (and there is also sort of an enemy that is following you if the player is within range) so now i want to shoot at this enemy. And i figured out that i could use the same colision detection that i already used now the only thing that seems to be a problem is to calculate a point 100 units in front of the player. to move the player i use this calculation: if (keys['W']){ xpos -= (float)sin(heading*piover180) * 2.5f; ypos -= (float)cos(heading*piover180) * 2.5f; if (keys['S']){ xpos += (float)sin(heading*piover180) * 2.5f; ypos += (float)cos(heading*piover180) * 2.5f; if (keys['A']){ heading -= 0.5f; if (keys['D']){ heading += 0.5f; that works just fine now I thougt that to calculate a point 100 units in front of the player I should use the same calculation but with a higher number at the end. something like this: xpos -= (float)sin(heading*piover180) * 100.0f; ypos -= (float)cos(heading*piover180) * 100.0f; sadly it doesnt work. do you have any suggestions? mfg

Share on other sites
Factoring!

const float angle = (90 - heading) * piover180;const vector2 direction(cos(angle), sin(angle));if (keys['W']) pos -= direction * 2.5f;if (keys['S']) pos += direction * 2.5f;if (keys['A']) heading -= 0.5f;if (keys['D']) heading += 0.5f;// 100 units ahead of playerconst vector2 ahead = pos + direction * 100f;

Also, standard mathematical formulas expect right to be at an angle of 0°, and top to be at an angle of 90°, so I suggest that you adapt your heading functions as well.

Share on other sites
but what do I have to include to use vector2?
I searched with google, but didnt get it to
work.

Share on other sites
vector2 was an arbitrary placeholder for a 2D vector class of your choice. You can look for such an online library online, or write it yourself as you go and replace it with a faster method if necessary.

For instance, you would only need the following to implement the code I provided, and you could add more functionality as the need arises.

struct vector2 {  float x, y;  vector2(float x, float y) : x(x), y(y) {}  vector2() x(0f), y(0f) {}  vector2 &operator+=(const vector2 &o)   {    x += o.x;    y += o.y;    return *this;  }  vector2 &operator-=(const vector2 &o)  {    x -= o.x;    y -= o.y;    return *this;  }   vector2 &operator*=(float s)  {    x *= s;    y *= s;    return *this;  }};const vector2 operator+(const vector2 &a, const vector2 &b){  vector2 c(a);  return (c += b);}const vector2 operator-(const vector2 &a, const vector2 &b){  vector2 c(a);  return (c -= b);}const vector2 operator*(const vector2 &a, float b){  return vector2(a.x * b, a.y * b);}const vector2 operator*(float a, const vector2 &b){  return b * a;}

Share on other sites
Your code as written should be working, moving xpos and ypos to be 100 units in front of where they previously were (or at least, consistent with how you are moving the player). Are you initializing xpos and ypos to be the player's current position before performing the operation? Try printing out their values at different places inside the function to see what effect your math is having.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• Forum Statistics

• Total Topics
634060
• Total Posts
3015300
×