Jump to content

  • Log In with Google      Sign In   
  • Create Account


Missile Pathing for 2D game


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
27 replies to this topic

#21 JTippetts   Moderators   -  Reputation: 8380

Like
1Likes
Like

Posted 05 January 2013 - 10:39 PM

I did a little test using Lua Love2D. alvaro is correct in that you don't need to keep a separate speed variable if you don't normalize your (vx,vy) velocity vector. Here is some quick video of the test using a field 640x480, initial missile velocity of (1,0), spring of 3 and damping of 1 against a circling target:
 
http://www.youtube.com/watch?v=1bQjN_6Xj2k
 
The missile updating code:
 

	local dx=target.x-missile.x
	local dy=target.y-missile.y

	local accelx=missile.spring*dx-missile.damping*missile.vx
	local accely=missile.spring*dy-missile.damping*missile.vy

	missile.vx=missile.vx+ dt*accelx
	missile.vy=missile.vy + dt*accely
	
	missile.x=missile.x+dt*missile.vx
	missile.y=missile.y+dt*missile.vy


It's pretty much just as written in his initial post, without normalization of (dx,dy) and without an external speed variable. You do have to play with the spring and damping constants quite a bit to get something that feels right.

Sponsor:

#22 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 06 January 2013 - 12:07 PM

It works now. Many thanks to all of you :)
And thanks for not giving up on me xD

Edited by P0jahn, 06 January 2013 - 12:35 PM.


#23 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 21 January 2013 - 08:21 AM

Bump!

There is an other thing, about the missiles rotation.

Currently in my game, the missile is always rotated so it is pointing at the target. Instead, I want it to rotate like the youtube click above posted by JTippetts.

In the clip, the missile rotates towards its direction.

 

How to achieve this?



#24 Álvaro   Crossbones+   -  Reputation: 12955

Like
0Likes
Like

Posted 21 January 2013 - 09:05 AM

Make the missile point in the direction of its velocity.

#25 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 21 January 2013 - 10:51 AM

Do you mean that the missile should point at the velocity vector?



#26 Álvaro   Crossbones+   -  Reputation: 12955

Like
0Likes
Like

Posted 21 January 2013 - 01:23 PM

I mean exactly what I said. You point at a point or in a direction; velocity is a vector, so you point in that direction.

#27 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 22 January 2013 - 09:35 AM

Oddly, that didnt work.

 

rotation = (float) getAngle(centerX, centerY, vx, vy);

public static double getAngle(float x1, float y1, float x2, float y2)
{
     float deltaX = x2 - x1;
     float deltaY = y2 - y1;

     return Math.atan2(deltaY, deltaX) * 180 / Math.PI;
}


#28 FLeBlanc   Crossbones+   -  Reputation: 3101

Like
1Likes
Like

Posted 22 January 2013 - 09:48 AM

The missile's direction wouldn't be from (centerx,centery)->(vx,vy). (vx,vy) indicate the direction the missile is going. That is the direction, and that is the vector you need.

 

angle=atan2(vy,vx)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS