More angular problems

Member
Posts: 105
Joined: 2007.03
Post: #1
Ok, I made this post on my previous thread and realized since I had been the last poster there for quite some time, that most people wouldn't bother to read it.

Anyways, I'm still very confused about this. Here is the code I'm using(or at least its the basic idea) to move the player around, maybe someone could tell me what I'm doing wrong still. Oh, and everything is done in the upper right quadrant of opengl.

float angle;
float radians

void move_player()
{

NSPoint vector;

vector.x = new_world_dst.x - player.x;
vector.y = new_world_dst.y - player.y;

angle = atan2(vector.x, vector.y);
radians = PI / 180 * angle;
position.x -= sin(radians) * SPRITE_SPEED;
position.y += cos(radians) * SPRITE_SPEED;
}
Quote this message in a reply
Member
Posts: 20
Joined: 2007.02
Post: #2
Does it throw an error or anything, or just a funny coord?
Just checked my angle-finding code (azimuth or something?), and I used
atan2(vectorY, vectorX);
(swapped arguments around)
Also, I added my cos value to position.y, and set normally had speed as negative (possibly had to do this because I swapped round the arguments for atan2)
That's what worked for me, good luck in finding a solution =]

Tobs
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #3
Using the same code I posted originally the values(cast as ints) for angle I get using atan2 go as follow:

NW through NE = 0
E = 1
SE = 2
S = 3
SW through W = -2
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #4
Anyone, please?
Quote this message in a reply
Member
Posts: 100
Joined: 2006.05
Post: #5
Try,

Code:
...
angle = atan2([b]vector.y, vector.x[/b]);
...
position.x [b]+= cos[/b](radians) * SPRITE_SPEED;
position.y += [b]sin[/b](radians) * SPRITE_SPEED;
...

At least that's how it should work in the first quadrant....
Quote this message in a reply
Member
Posts: 105
Joined: 2007.03
Post: #6
Actually I figured out that atan2 returns in radians so, instead of using radians I use the angle:

angle = atan2(vector.y, vector.x);
position.x -= cos(angle) * SPRITE_SPEED;
position.y += sin(angle) * SPRITE_SPEED;

Seems to work fine now, thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Combining then splitting angular and linear velocity reubert 4 4,924 Sep 5, 2006 12:47 AM
Last Post: reubert