Finding the angle between two sprites

Started by
15 comments, last by randomDecay 21 years, 8 months ago
hello there. I want my enemy to chase down the player by finding the angle between the two and setting its vector to that angle. Here's what I am doing:
  
float opp = ( p_playerY - enemyY );
float adj = ( p_playerX - enemyX  );

float angle = atan( ( opp / adj ) ) * 180.0f / 3.14f;
   
This should give me the angle right? well, the enemy doesn't move toward the player like he should. Can someone help or give another solution to this problem? Thanks. [edited by - randomDecay on August 15, 2002 2:06:46 PM] [edited by - randomDecay on August 15, 2002 2:07:12 PM]
Advertisement
Off the top of my head I''d say it''s something to with operator precedence. Try using this


  float opp = ( p_playerY - enemyY );float adj = ( p_playerX - enemyX  );float angle = (atan( ( opp / adj ) ) * 180.0f) / 3.14f;  
damn, didn''t help...
atan2( y, x ) yields the angle.
Alternativelly, you can use do this:

dir_x = ai_x - player_x
dir_y = ai_y - player_y;

dir_x & dir_y are now the direction vectors the ai should travel along to reach the player.

ie ai_x + dir_x equals player_x

To move the ai one step towards the player over time ( dt ), then do something like

ai_x += dir_x * dt;
ai_y += dir_y * dt;


These operations would all be easier if you use a vector library.
maxsteel -- your second solution will work thanks, but I have one question for you about it...

let''s say enemy XY = (96, 96) player XY = ( 416, 288 )

dirx = 96 - 416 = -320
diry = 96 - 288 = -192

you said enemyX + dirX = playerX which is not true :
96 + -320 = -224

On a computer screen the enemy should be going SOUTH EAST with the coordinates above. but if you work out your solution, ie --

enemyX += ( dirX * dt ) (let''s say dt = 1/2 )

enemyX = 96 + ( -320 * .5 ) = -64 ?? Shouldn''t the enemy be going EAST in the x direction?

Thanks and please tell me if I understand what I am doing
Try this:

dir_x = player_x - ai_x;
dir_y = player_y - ai_y;

(Flip the operands in the subtraction)
It's not what you're taught, it's what you learn.
sweet, it works perfectly! One thign I am concerned about though: all my movement for enemies is INDEPENDENT of the frame rate. Is this method dependent on the framerate? I can''t seem to get that atan2 method working, so I guess I am going to have to use this.

Thanks maxsteel and everyone else who helped!
if you divide dir_x and dir_y by sqrt(dir_x^2 + dir_y^2), you will get the same values for the x and y offset that you would have gotten if your atan2 method was used, and then used sin and cos on the angle to generate an x and y offset, if that answers your question.

As for being frame rate dependent, that depends on if you base movement on the system clock (frame rate dependent), or plan to set a strict amount of movement each frame (not frame rate dependent).

if you want your movement to be independent of frame rate, you will always multiply your x and y offset values by the same value for movement.

Make sense?


[edited by - Waverider on August 15, 2002 3:43:57 PM]
It's not what you're taught, it's what you learn.
so if I always use the same dt, it will be the same offset even on two different computers with different framerates?

But the way I see it is with a higher framerate, the PursuePlayer() function will be called more more often than a lower framerate. So the enemy will move faster on some machines with higher framerates.

[edited by - randomDecay on August 15, 2002 3:49:20 PM]
Any game that moves a set amount each frame will move faster on a higher end machine if the frame rate itself isn't controlled somehow. (EVERYTHING will move faster)

If you want to, say, limit the frame rate to 24, then you could keep the object positions from updating until 1/24 second in time passes, then allow the movement routine to execute on all the objects.

Otherwise, you can update the object positions every frame based on how much time has actually elapsed since the last frame, and the objects will move the same speed no matter what machine they are running on.

To answer your question, if you use the SAME dt every frame, a faster machine will run the movement routine more often, causing everything to go faster. So if you base your dt on how much time elapses instead of setting it the same value every frame, you will get the same movement speed on every machine, even thought the frame rate will be higher on the faster machines (IF you don't want to control the frame rate).

[edited by - Waverider on August 15, 2002 4:13:02 PM]
It's not what you're taught, it's what you learn.

This topic is closed to new replies.

Advertisement