Rotary/Seeking Missiles

Mh, maybe you can try it with a touch joint like mentioned in the corona tutorial for radial gravity?

http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/

Btw. a great website you posted there SegaBoy. Thanks for sharing!

Yeah, the homing missile part wouldn’t be hard - I don’t want just a homing missile. Look at this video and there’s an example of it:

http://www.youtube.com/watch?v=WmoxbJ4NvQs

Starting at about 4:45 it gets to the best examples of what I’m looking for.

I’m guessing no answers because this is very difficult…?

  • C

Mh, for me that looks relativly random.

I think something like the following is happening.

1 - a target is choosen

2 - the missle flys at least a half circle from it’s starting position

3 - missle flys in a circle til it has the right angle to fly directly forwards to the target

4 - if the target is destroyed while the missle is in the air it restarts from point 1

It’s not easy but possible. You have to roate the missle around a specific point via a sin function.

You have to set up an enter frame listener for each missle to track its position in relation to its target and to check if the target is still alive.

And if you want the same as shown in the video you have to add some randomness. So the circles the missles fly should have different radia and the missles sometimes fly a circle without any reason.

The only problem that I see is the performance if you have like 50 missles like that. Cause you have track distance, rotation and some other stuff in an enter frame loop. could be a bit too hard for corona, cause we don’t have low level math functions for that kinda stuff.

@Caleb,

Hi.  I added an additional solution to the Just Mechanics Kit: http://gum.co/RCpe/  

It works a little bit more like what you showed in the video above.  It uses linear velocity and turns at a fixed number of degrees-per-second.

http://www.youtube.com/watch?v=ve4Lk48sQtI

Why does the missile path keep extending after collision?  The ship and missile are hidden, so the missile keeps moving for a bit till the loop destroys it.  You would of course destroy the missile and ship in your game, but this is just a demo.

Note:  As a side-effect of not destroying the missile on collision, it keeps scanning for new targets and on the rare occasion when one is provided, it ‘jinks’ in the direction of that target.  This happens because there was a long time-delta since the last turn.  You can stop this by capping the time delta  in the turning function, or by capping the maxTurn calculation.  However, in most uses of this algorithm, the missile will be destroyed and unable to find a new target.

Note 2: What if you want a missile to ‘accelerate’?  You can still use linearvelocity, but over time raise the missile speed till you hit your max (desired) velocity.  You could use apply force, but using linearvelocity while turning is easier and in my opinion it looks nicer.

Note 3: For the very observant, yes, those markers are being created at the <x,y> centroid of this ship and are not meant to simulate a smoke trail.  They are purely to show where the missile is and has been. :slight_smile:

Update: torbenratzlaff has a point about performance but I would approach it like this:

  1. Solve the core issue/mechanic.

  2. Test in on worst case scenarios.  

If there is a problem, continue…

  1. Locate the hot spots in your calculation(s) using a profiler.

  2. Optimize the worst offenders.  There are lots of things you can do to speed up the calculations in the JM002 package.

  3. Test, and repeat.

I’m sure it is possible to make a sample with 50+ moving objects that are seeking targets (minus any special effects).

@roaminggamer:

That’s exactly what I’m looking for. [see below]

@Everyone Else:

For the sake of learning, I’m trying to achieve it myself. With torbenratzlaff’s pointers, I’ve come up with this… The only problem is that it always tries to get the lowest angle, so it “wiggles” when the angle between the knatch and the missile goes back and forth. Any suggestions for how to solve this?

For the record, “knatch” is a word I made up to mean object, boid, thingy, or anything else that moves in a game. Boxes are knatches. Pillbugs are knatches. Etc., etc., etc.

[lua]

local deg = math.deg

local rad = math.rad

local atan2 = math.atan2

local sin = math.sin

local cos = math.cos

local function angleBetween(a, b)

    return deg(math.atan2(b.y - a.y, b.x - a.x)) + 90

end

local function forcesByAngle(totalForce, angle)

    local forces={}

    local radians= -rad(angle)

    forces.x = cos(radians) * totalForce

    forces.y = sin(radians) * totalForce

    return forces

end

local function smallestAngleDiff(target, source)

    local a = target - source

    if (a > 180) then

        a = a - 360

    elseif (a < -180) then

        a = a + 360

    end

    return a

end

local function clamp(n, l, h)

    if n < l then

        return l

    elseif n > h then

        return h

    else

        return n

    end

end

local knatch = display.newCircle(0, 0, 20)

knatch.x, knatch.y = 128, 384

knatch.xSpeed = 3; knatch.ySpeed = 0

local missile = display.newRect(0, 0, 60, 10)

missile.x, missile.y = 512, 512

missile.xSpeed, missile.ySpeed = 0, 0

missile.firingSpeed = 6

missile.maxTurningAngle = 4

local function onRuntime(event)

    knatch:translate(knatch.xSpeed, knatch.ySpeed)

    missile:translate(missile.xSpeed, missile.ySpeed)

    local angle =  math.abs(clamp(angleBetween(missile, knatch) + 90, missile.rotation - missile.maxTurningAngle, missile.rotation + missile.maxTurningAngle))%360

    

    missile.rotation = angle

    local forces = forcesByAngle(missile.firingSpeed, math.abs(angle))

    missile.xSpeed, missile.ySpeed = -forces.x, forces.y

    if knatch.x > display.contentWidth then

        knatch.xSpeed = -3

    elseif knatch.x < 0 then

        knatch.xSpeed = 3

    end

end

Runtime:addEventListener(“enterFrame”, onRuntime)

[/lua]

  • Caleb

@Caleb,

I sent a DL code your way… Cheers.

@roaminggamer

Is it possible to use a physics function, such as (missile.angularVelocity) to rotate the missile? I am trying to use your code on a multi element body, but because the rotation is changed directly it is causing problems with any joints attached to the main body.

Any advice?

I just found this post - what an awesome solution!

@roaminggamer

Is it possible to use a physics function, such as (missile.angularVelocity) to rotate the missile? I am trying to use your code on a multi element body, but because the rotation is changed directly it is causing problems with any joints attached to the main body.

Any advice?

I just found this post - what an awesome solution!