Simple Issue- make object spawn at point, move toward target obect, trigger event on hit object

Hi all, thanks for any help-

I’m moving over from Unity and JavaScript, having a lot more trouble than expected, sadly.

Essentially, I’d like to have some objects randomly spawn near the top of the screen, rotate themselves to “point” towards a target object (the player, naturally), and then move towards that target at a set speed per second (not per frame!). The player will not move, so these objects only need to calculate an initial orientation, and then move forward till they hit the player, and then trigger an event.

Now, in JS, I would do something like:

Function SpawnEnemy()
{
newEnemy = Object.Instantiate(enemyPrefab, someX, someY);
newEnemy.LookAt(thePlayer);
allEnemies.Add(newEnemy);
}

Function Update()
{
for (theEnemy in allEnemies)
{
transform.translate(enemySpeed * Time.deltaTime);
}
}

Simple enough to me there, but I’m really hitting a brick wall trying to get this working in Lua/Corona.

I’d really appreciate any pointers on this, thank you much! [import]uid: 69255 topic_id: 11238 reply_id: 311238[/import]

I’m too new to Corona to help with the rotation and collision detection, but in my only other iPhone game, I used vector subtraction to have one object follow or track another. velocity and location are members of the chaser, target is the coordinates of the chased (0 index is x, 1 index is y).

The important part is the velocity = target - location vector subtraction. The rest just regulates the speed of travel.

[code]

void Swarm::setVelocity()
{
if (hasTarget()) {
velocity[0] = target[0] - location[0];
velocity[1] = target[1] - location[1];
m3dNormalizeVector2(velocity);
float len = m3dGetVectorLength2(velocity);
if (len < maxSpeed) {
while (len < maxSpeed) {
m3dScaleVector2(velocity, 1.05);
len = m3dGetVectorLength2(velocity);
}
} else if (len > maxSpeed) {
while (len > maxSpeed) {
m3dScaleVector2(velocity, 0.95);
len = m3dGetVectorLength2(velocity);
}
}
} else {
velocity[0] = velocity[1] = 0.0;
}
}
[/code] [import]uid: 58455 topic_id: 11238 reply_id: 40741[/import]

thanks dave- unfortunatly that’s just going wayyy over my head, lol.

Can you/another show a simple, idiot proof, method of just performing a “LookAt(target)”? It seems so simple…yet I’m just not seeing it.

Thanks much! [import]uid: 69255 topic_id: 11238 reply_id: 40853[/import]

I don’t know if there’s a Corona API to do this, but here’s a simple way to implement one object tracking or chasing another. You’ll have to use your own images - mine are 32x32. Save to main.lua and run in the simulator. The red orb will chase the yellow one wherever it goes.

[code]
local physics = require(“physics”)
physics.start()

local redOrb = display.newImage(“RedGlass.png”)
local yellowOrb = display.newImage(“YellowGlass.png”)
local SPEED = 0.3

– put redOrb at top left
redOrb.x = redOrb.contentWidth/2
redOrb.y = redOrb.contentHeight/2

– put yellowOrb middle right
yellowOrb.x = display.contentWidth - yellowOrb.contentWidth/2
yellowOrb.y = display.contentHeight/2

physics.addBody(redOrb, “kinematic”,
{ friction=0.0, bounce=0.0, density=0.0, radius=redOrb.contentWidth/2.0 }
)

physics.addBody(yellowOrb, “kinematic”,
{ friction=0.0, bounce=0.0, density=0.0, radius=yellowOrb.contentWidth/2.0 }
)

– move towards the left at constant velocity
yellowOrb:setLinearVelocity(-15, 0)

function gameLoop(event)
– constantly adjust velocity to track yellowOrb
redOrb:setLinearVelocity(
SPEED * (yellowOrb.x - redOrb.x),
SPEED * (yellowOrb.y - redOrb.y)
)
end

Runtime:addEventListener(“enterFrame”, gameLoop)
[/code] [import]uid: 58455 topic_id: 11238 reply_id: 40875[/import]

Thanks so much, that really helps! [import]uid: 69255 topic_id: 11238 reply_id: 40879[/import]