PDA

View Full Version : Sprite with 24 images needs direction formula?



Firlefanz
14-01-2006, 08:15 AM
Hi!

I have a star ship with 24 images, every image has a rotation degree.

So I need to know:

Starship is located x1,x2.

Player (the starship should 'look' at the player) is x2,y2.

How do I order the images?
Perhaps imageindex 0 is to top, then 1 the next to top right and then clockwise arranging?

How do I get the imageindex so it 'looks' at x2,y2?
Understandable?

Thanks,
Firle

dmantione
14-01-2006, 08:47 AM
Add an orientation angle to each image, with respect to an imaginary vertical line. Draw an imaginary line between both sprites. Calculate the angle between that line and the imaginary vertical line. Select the sprite with the orientation angle that is closest to the calculated angle.

Firlefanz
14-01-2006, 11:06 AM
Hi!

Thanks for the reply. But I think there is a much easier way.
I saw once a formula for something like this to calculate the imageindex...

Don't remember but it should be possible to calculate this...
from the coordinates.

Firle

Paulius
14-01-2006, 11:33 AM
dmantione told you all you need to know, for a more code like explanation:
Dir:= normalize(LookAt ?˘_" IsAt);
imageNr:= Round(ImageCount / 2 / pi * Arccos(Dir.y));
if (Dir.x > 0) then imageNr:= ImageCount ?˘_" imageNr;

Firlefanz
14-01-2006, 11:48 AM
I have to admit I am bad at maths. :oops:
Especially very bad in geometry (correct word?)

I am having only X,Y coordinates in a 2D game.

So ok I need the angle first. How do I calculate it using X1,Y1 standing location and X2,Y2 location to look at? (normalize???)

If I have the angle, I can calculate the imageindex.
0AŹ? is image 0, 360/24 should also be 0.

But how to get the angle?

Firle

dmantione
14-01-2006, 12:02 PM
Draw a triangle between (x1,y1), (x2,y1) and (x2,y2). Then you can do simple goniometry to calculate the angle. I didnt check, but I am convinced you will end up with the simple arccos Paulius used in his formula.

Nitrogen
14-01-2006, 01:32 PM
Here is my code that I use to get an angle:



Angle := round((180 * (1 + ArcTan2((X1-X2), (Y1-Y2)) / PI)));
if Angle < 0 then Angle &#58;= 360-Angle;
if Angle > 360 then Angle &#58;= Angle-360;



ArcTan2 and PI defined in Math.pas :)

Then you just need something like this:



ImageNumber &#58;= round&#40;&#40;Angle/360&#41;*NumberOfImages&#41;;

Firlefanz
14-01-2006, 01:53 PM
Hi!

Thanks Nitro,

exactly what I wa slooking for, great! :D

Firle

Firlefanz
14-01-2006, 02:35 PM
Working perfect now and looking very sweet.

Thanks a lot guys! :D

Firle