Object chasing

Hi all

Very new to Corona but I’m enjoying the challenge :slight_smile:

I was wondering how I would go about getting an object to target and chase a static object (like a homing missile)? If anyone can point me in the right direction that would be great.

Many Thanks
Oz [import]uid: 91527 topic_id: 16965 reply_id: 316965[/import]

Hi Oz, there are many ways to achieve that kind of thing.

If the object is static, as in it does not move, you could use a transition.to() very easily, like so;

[lua]transition.to(missile, {x=target.x, y=target.y, time=1000})[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 16965 reply_id: 63726[/import]

Hi Peach

Thank you for the reply I will give that a go.

By the way great tutorials, I’m going through them (:

Oz [import]uid: 91527 topic_id: 16965 reply_id: 63837[/import]

No worries, I’m glad you’ve been enjoying my tutes :slight_smile:

Peach :slight_smile: [import]uid: 52491 topic_id: 16965 reply_id: 63964[/import]

Hi Peach

The transition.to worked a treat for the dynamic chasing the static :slight_smile:

I’ve had a look at the resource docs and searched the forums for a solution to a dynamic chasing dynamic, would a transition.to be able to achieve the dynamic chase?

Thanks
Owen [import]uid: 91527 topic_id: 16965 reply_id: 64242[/import]

Hi Oz
As far as I am aware dynamic chasing dynamic using a transition.to doesn’t work.
The other issue with transition.to is that the time it takes to reach the target will be the same regardless of distance, so the further way the target is the faster the chasing object will move.
To get around this what I do is to work out the angle between the two objects and then use some weird maths magic to move the objects like thus…
 

local angle= math.atan2(target.y - missile.y, target.x - missile.x) -- work out angle between target and missile missile.x = missile.x + (math.cos(angle) \* missile.speed) -- update x pos in relation to angle missile.y = missile.y + (math.sin(angle) \* missile.speed) -- update y pos in relation to angle

The bonus of this method is that it works regardless of wether the target is moving or not, and the missile will always move at a constant speed (unless you increase it).

Hope that helps. [import]uid: 7841 topic_id: 16965 reply_id: 64303[/import]

Hi Appletreeman (like the name :slight_smile:

I will have ago with your suggestion. I’m liking the learning curve with Corona and Lua.

Thank you for the reply
Oz [import]uid: 91527 topic_id: 16965 reply_id: 64322[/import]

Hello

Tried the code posted and it works in that it targets but it doesn’t move towards the object like the transition.to does.

I’ve tried changing the speed high and low for the missile but instead of a gradual speed towards the object the missile would instantly jump to a different position on screen once. Move the object and the missile wouldn’t move.

I’m starting to think I need a loop for the missile but also a way of getting the missile to move and track the object.

Oh it’s all good fun :smiley:

Thanks
Oz [import]uid: 91527 topic_id: 16965 reply_id: 64537[/import]

Ah right yeah, I sould have explained a bit more.
The code does need to be in a loop for it to work.
In my app I have a main game loop that calls various functions like this…
[lua]Function mainGameLoop()
updatePlayer()
updateSpawner()
updateEnemy()
checkfood()
Particles.Update()
removeTheDead()
end[/lua]
So then in the updateEnemy function I would have the code I posted earlier.
As the game is run it uses an enterframe event to check various conditions (gamePaused, gamePlaying, gameOver etc.) and if the gamePlaying boolean is True then it calls the mainGameLoop function.

I hope this makes sense to you as I’m not very good at explaining things. [import]uid: 7841 topic_id: 16965 reply_id: 64572[/import]

Yes that makes a lot of sense :smiley: You explain very well.
I shall get to work trying it and report back.

Thank you
Oz [import]uid: 91527 topic_id: 16965 reply_id: 64663[/import]

Ok, so I’m a little late to the conversation but this has raised a question that I have.

In regards to the posts using transition, ‘transition.to’ would work if you were launching one object at a time but say you wanted to have the player select multiple targets and have all of the objects/missiles fire at once? [import]uid: 102017 topic_id: 16965 reply_id: 65837[/import]