Sign in to follow this  
settis

calculating with sin and cos

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 doesn`t work. do you have any suggestions? mfg

Share this post


Link to post
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 player
const 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 this post


Link to post
Share on other sites
thx for the reply
but what do I have to include to use vector2?
I searched with google, but didn`t get it to
work.

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this