• 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.

27 replies to this topic

### #21JTippetts  Moderators

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

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.

### #22P0jahn  Members

305
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.

### #23P0jahn  Members

305
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  Members

20254
Like
0Likes
Like

Posted 21 January 2013 - 09:05 AM

Make the missile point in the direction of its velocity.

### #25P0jahn  Members

305
Like
0Likes
Like

Posted 21 January 2013 - 10:51 AM

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

### #26Álvaro  Members

20254
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.

### #27P0jahn  Members

305
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;
}

### #28FLeBlanc  Members

3133
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.