• Advertisement
Sign in to follow this  

Ship movement + Guiding projectiles toward ship

This topic is 1277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

 

I have the following code where Projectiles are guided toward the ship.

DeltaX = (_ShipPosX + _Ship.Width / 2) - Projectile.PositionX;
DeltaY = (_ShipPosY + _Ship.Height / 2) - Projectile.PositionY;
Angle = Math.Atan2(DeltaY, DeltaX) * 180 / Math.PI;

Projectile.SetPosition(Projectile.PositionX += Math.Cos(Angle) * General.MonsterProjectileSpeed, Projectile.PositionY += Math.Sin(Angle) * General.MonsterProjectileSpeed);

It is fine when the ship doesn't move but when the ship starts to move, the projectile is rotating around itself.  I'm wondering what I'm missing to make the projectile continue its course.  I tried to figure this out since a week or two now without success.  I was hoping someone could shed some light on it.

 

You can see the effect here : https://skydrive.live.com/redir?resid=7056CFFE05236B7D!919&authkey=!AF1Ef24BdnDgYT4&ithint=video%2c.mp4

 

Thank you for any help.

Edited by GearWorld

Share this post


Link to post
Share on other sites
Advertisement

If Math.Atan2 returns an angle in radians, then it's entirely probable that Math.Cos and Math.Sin expect the angles passed in to be in radians as well.

Share this post


Link to post
Share on other sites

That's why there's * 180 / Math.PI and this is put in a variable in Radian and passed to COS and SIN.

This formula works ok and it follows the ship all right but when the ship moves it's not suppose to rotate around itself during the ship moving but just continue its path to follow the ship since both moves !

Share this post


Link to post
Share on other sites

You are converting the result of atan2() to degree !

 

"Angle = Math.Atan2(DeltaY, DeltaX) * 180 / Math.PI;"

 

So, you need to change it to 

 

"Angle = Math.Atan2(DeltaY, DeltaX);"

Share this post


Link to post
Share on other sites

Angle would be needed only to orient a sprite (e.g. an oblong rocket) or to simulate rotational dynamics. The code example can be simplified to use vectors and normalize the length of (DeltaX,DeltaY) without trigonometric functions:

DeltaX = (_ShipPosX + _Ship.Width / 2) - Projectile.PositionX;
DeltaY = (_ShipPosY + _Ship.Height / 2) - Projectile.PositionY;

Scale=General.MonsterProjectileSpeed / Math.Sqrt(DeltaX*DeltaX+DeltaY*DeltaY);

Projectile.PositionX += DeltaX*Scale; 
Projectile.PositionY += DeltaY*Scale; 

Share this post


Link to post
Share on other sites

You are converting the result of atan2() to degree !

 

"Angle = Math.Atan2(DeltaY, DeltaX) * 180 / Math.PI;"

 

So, you need to change it to 

 

"Angle = Math.Atan2(DeltaY, DeltaX);"

 

This works fine.  Thank you very much !

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement