Results 1 to 9 of 9

Thread: Sprite with 24 images needs direction formula?

  1. #1

    Sprite with 24 images needs direction formula?

    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

  2. #2

    Sprite with 24 images needs direction formula?

    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.

  3. #3

    Sprite with 24 images needs direction formula?

    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

  4. #4

    Sprite with 24 images needs direction formula?

    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;

  5. #5

    Sprite with 24 images needs direction formula?

    I have to admit I am bad at maths. ops:
    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

  6. #6

    Sprite with 24 images needs direction formula?

    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.

  7. #7

    Sprite with 24 images needs direction formula?

    Here is my code that I use to get an angle:

    Code:
      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:

    Code:
    ImageNumber &#58;= round&#40;&#40;Angle/360&#41;*NumberOfImages&#41;;
    My site: DelphiTuts.com (coming soon)...

    Download Font Studio 4.21 here.

  8. #8

    Sprite with 24 images needs direction formula?

    Hi!

    Thanks Nitro,

    exactly what I wa slooking for, great!

    Firle

  9. #9

    Sprite with 24 images needs direction formula?

    Working perfect now and looking very sweet.

    Thanks a lot guys!

    Firle

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •