Need help figuring out the angle between 2 sprites...

Find a Bug? Have a Problem? Like to Suggest a Feature? Do it Here

Moderators: g3nuin3, SpeedWing, WhiteHat, mezzo

Need help figuring out the angle between 2 sprites...

Postby esco » Tue Jan 13, 2009 7:14 am

What I'm basically trying to do is figure out the angle between a projectile and the player. I have the horizontal and vertical positions of both on screen, I just don't know the formula to use.

Then based on that I can figure out the horizontal and vertical speed of the projectile. So for example:

if the angle between them both is 0 degrees (player is DIRECTLY in front of, or behind, depending on your view) projectile, the hspeed will be 400000, vspeed will be zero. So it goes straight forward.

if the angle between them both is 90 degress (player is DIRECTLY aboveor below) projectile, the hspeed will be 0, vspeed will be 400000. So it goes straight down (if I want up I just make vspeed -400000).

If the angle is 45 degress the speed would be hspeed is 300000, vspeed is 300000 (these are negative for left, and up respectively).

Once I have the angle figured out I already have made the code for how to calculate the speed based on the angle, whether it's right or left, up or down etc. But I canNOT figure out how to get the game to calculate the angle of the player in reference to the projectile so it is thus aimed at him/her.

I've looked online and found some code snippets involving arctan, but those won't work here. Can anyone help me in creating this code to figure out what angle the player is in reference to the projectile?
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Postby L. Spiro » Tue Jan 13, 2009 12:15 pm

Code: Select all
struct POINT2D {
    FLOAT x;
    FLOAT y;
};


// Set these accordingly.
POINT2D pSrc;
POINT2D pTarget;



// Then do the math.
POINT2D pDelta = { pTarget.x - pSrc.x, pTarget.y - pSrc.y };
FLOAT fDeltaLen = pDelta.x * pDelta.x + pDelta.y * pDelta.y;
// fDeltaLen is 0 if target and source are the same.
if ( fDeltaLen > 0.0f ) {
    FLOAT fInvLen = 1.0f / Sqrt( fDeltaLen );
    pDelta.x *= fInvLen;
    pDelta.y *= fInvLen;
}
else {
    pDelta.x = 0.0f;
    pDelta.y = 0.0f;
}


pDelta.x *= 400000.0f;
pDelta.y *= 400000.0f;


// pDelta now contains the horizontal (x) and vertical (y) velocities to
//  apply to the bullet.


Donations please.


L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Postby esco » Wed Jan 14, 2009 10:28 am

Coo. Thanks for the help bro. I was trying to use ATan but it kept saying that there was no such argument when I compiled it

L. Spiro wrote:
Donations please.


L. Spiro


......... did you just ask me for money dawg? lol I gave a while back. Which I never, EVER do for ANYONE, ANYWHERE online. But in your case, since you have been so helpful I did. :)

Actually, now that you mention it, I would be willing to pay someone who can make me a few tools to make editing symphony of the night easier. And you OBVIOUSLY are very, very skilled.

How much would you charge for example to make a tool that could decrypt the sprite gfx from the iso (I know someone who has the decryption cracked already. he could provide that info) but that can also re-encrypt gfx in over the old gfx in the iso? Allowing me to change how enemy sprites look in game?
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Postby L. Spiro » Wed Jan 14, 2009 1:25 pm

Asking for money was satire. I almost wrote, “Donations plz.”

I have too much to do on my own projects combined with a full-time job, as well as full-time dating of scores of women.

The price for dragging me away from my own projects (which I need as résumé work) is far too much for one person.

But making such a tool would be simple with L. Spiro Script. I am sure you would not need much help.


L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Postby esco » Thu Jan 15, 2009 11:00 am

L. Spiro wrote:Asking for money was satire. I almost wrote, “Donations plz.”


Liar. You hustlin! ;)

I have too much to do on my own projects combined with a full-time job, as well as full-time dating of scores of women.


... you must be rich. Or hung like a rhino.

The price for dragging me away from my own projects (which I need as résumé work) is far too much for one person.


I'll give you 20 american dolars. That's like a 10000000000 in your country, isn't it? :P

But making such a tool would be simple with L. Spiro Script. I am sure you would not need much help.


BWA HA HA HAAAAA! Thanks for the vote of confidence. But I wouldn't know where to start. I couldn't even figure out how to do code for an angle without asking for help. :( I really need & want to learn more stuff, but I'm so busy with work and life that I barely have the time.

L. Spiro


Who? lol
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Postby L. Spiro » Thu Jan 15, 2009 11:30 am

Rotations are a common place to get stuck for many people. The math behind them is a bit abstract and you are not the only person to ask me for help with them within the last week alone.

I doubt you will have to rotate the files. Unpacking and repacking is tedius but simple. One step at a time.


L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Postby lxcid » Wed Jan 21, 2009 1:35 am

If you want to learn math, especially in the field of games and graphics, 3D Math Primer For Games and Graphics Development is my recommendation.

What L.Spiro wrote in his code is actually does this.

1) By subtracting target point from source point, you essentially get a vector from source to target, which is called delta.
2) He get the squared length of delta. The reason probably being square root is a expensive operation.
3) If the squared length of delta is 0, it probably means that the source and target is at the same point (The only possible scenario when squared length is 0), so delta is set to 0.
4) Else if the squared length is not 0, he would normalize the delta to a unit vector.
5) He then times the delta by 400000. which you can now use this vector to move your sprite.

I was actually reading about this in the book not long ago. So it kinda like an exercise for me. :X

Cheers. :)
lxcid
I Ask A Lot Of Questions
 
Posts: 13
Joined: Tue Jan 20, 2009 2:07 am

Postby esco » Thu Jan 29, 2009 10:48 am

lxcid wrote:If you want to learn math, especially in the field of games and graphics, 3D Math Primer For Games and Graphics Development is my recommendation.

What L.Spiro wrote in his code is actually does this.

1) By subtracting target point from source point, you essentially get a vector from source to target, which is called delta.
2) He get the squared length of delta. The reason probably being square root is a expensive operation.
3) If the squared length of delta is 0, it probably means that the source and target is at the same point (The only possible scenario when squared length is 0), so delta is set to 0.
4) Else if the squared length is not 0, he would normalize the delta to a unit vector.
5) He then times the delta by 400000. which you can now use this vector to move your sprite.

I was actually reading about this in the book not long ago. So it kinda like an exercise for me. :X

Cheers. :)


I might just check that book out the next time I head by barnes and nobles. I actually have only math classes left in college which is what is causing me issues. I got up to pre-calc YEARS ago, with no problems, but since I stopped going to school for about 5 years I remember NOTHING of these types of math. Which makes continuing my education VERY hard. :( A book like this won't just help me at home, it might help me at school too! :)
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Re: Need help figuring out the angle between 2 sprites...

Postby esco » Sun Jan 17, 2010 5:00 am

Hey bro, I am having some trouble doing something similar to the code I asked for help with above. Now I am actually just trying to get the angle itself between the 2 sprites in degrees. I did some research, but all the code that I try doesn't work right. Here is the code I am using but it doesn't appear to give me the correct angle:

Code: Select all

float pi = 3.1415963f;
//calculates out the angle in degrees
FLOAT angle = (ATan(theplayer.y/theplayer.x)-ATan(projectile.y/projectile.x)) * (180/pi);
//basically makes sure angle value comes out positive
if (angle < 0) {angle*=-1;}


NOTE: that this is just needed for both sprites being in one quadrant; when the player is above and in front of the creature shooting the fireball. if player is below or behind the creature shooting the projectile, it is set to just be fired forward and not rotated.

I need the angle in degrees because in SOTN for the rotation value, to rotate 1 degree is about 11.38. So if I want to rotate a sprite 45 degrees the value is 512.
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Re: Need help figuring out the angle between 2 sprites...

Postby esco » Mon Jan 25, 2010 9:07 am

You are kidding me right? In a place full of programming guru's NO ONE here can help me? Or is my question just that dumb, that no one wants to answer it? :cry:
Esco.... the name says it all. New Yorikan for life.
User avatar
esco
NULL
 
Posts: 148
Joined: Mon Sep 18, 2006 2:25 am
Location: Florida, a.k.a. the US's version of hell!

Re: Need help figuring out the angle between 2 sprites...

Postby L. Spiro » Mon Jan 25, 2010 10:15 am

I read this before going to work or something when I did not have time to answer, and by the time I had the time to answer, I forgot about it.



Anyway, degrees relative to 2 objects in absolute coordinates (relative to the screen):
Code: Select all
Vector2 vDif = {
    eEnemy0.x - eEnemy1.x,
    eEnemy0.y - eEnemy1.y,
};
FLOAT fAngle = ATan2( vDif.y, vDif.x ) * (180.0f / 3.1415963f);
WORD wFinalRotationValue = (WORD)(fAngle * 4096f) & 0xFFF;



If you need the position of your player relative to the direction the enemy is facing there is a simple way using dot products.
But since it requires (simple) helper functions I will wait until you actually confirm you need it to provide the code.


L. Spiro
Our songs remind you of songs you’ve never heard.
User avatar
L. Spiro
L. Spiro
 
Posts: 3129
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan


Return to Bugs/Problems/Suggestions

Who is online

Users browsing this forum: No registered users and 0 guests