Moving an object in the direction it's pointing

I am adding object to the screen and randomly setting the rotation of each object. I want to make each one move forward in the direction that it is pointing but can’t seem to figure it out. I’ve googled it but to no avail. Any help would be greatly appreciated.

Thanks,

Jason [import]uid: 136162 topic_id: 24656 reply_id: 324656[/import]

I’m not certain but I believe Rum (available from code exchange) has some help with functions to simplify the math on this - it was made by the creator of Line (Graham Ranson) and is well worth taking a look at :slight_smile: [import]uid: 52491 topic_id: 24656 reply_id: 100071[/import]

Thanks Peach!

Do you know where it is? I couldn’t find it. Is the title Rum?

Jason [import]uid: 136162 topic_id: 24656 reply_id: 100113[/import]

Here’s an adaptation of a starfield effect that I created a while back. The code isn’t particularly elegant, but it works and can be used as a starting point. I don’t even begin to understand the maths involved so can’t explain it :slight_smile:
Here’s the png for the arrow I used if you want it to test.

[lua]display.setStatusBar (display.HiddenStatusBar)
_m=math.random
_w=display.contentWidth – Stores the width of the screen
_h=display.contentHeight – Stores the height of the screen
maxstars=30 – Sets the number of objects to be created

function createStars()
stars={}
for f=1, maxstars do
stars[f]= display.newImage(“arrow.png”) – I just used an image of an arrow so it’s easier to see the direction it is pointing
stars[f].x=_w/2 – Set start position to centre of screen
stars[f].y=_h/2 – Set start position to centre of screen
stars[f]:setFillColor(_m(1,255),_m(1,255),_m(1,255)) – Random colour (not really needed, but looks pretty)
stars[f].angle=math.atan2(_m(_h) - stars[f].y, _m(_w) - stars[f].x) – sets the initial angle to a random figure (I don’t understand maths, but it works)
stars[f].speed=_m(5,15) – Random speed
stars[f].rotation=(stars[f].angle*(180/math.pi)) – Rotates the object to point in the correct direction. An angle of 0 means the arrow is pointing left to right.
end
end

function updateStars()
for u=1, #stars do
stars[u].x= stars[u].x + (math.cos(stars[u].angle) * stars[u].speed) – Updates X position
stars[u].y= stars[u].y + (math.sin(stars[u].angle) * stars[u].speed) – Updates Y position
if stars[u].x< 0 or stars[u].x > _w or stars[u].y< 0 or stars[u].x > _h then – Checks if object is outside the bounds of the screen
stars[u].x=_w/2 – Sets X back to centre of screen
stars[u].y=_h/2 – Sets Y back to centre of screen
stars[u].angle=math.atan2(_m(_h) - stars[u].y, _m(_w) - stars[u].x) – New random angle
stars[u]:setFillColor(_m(1,255),_m(1,255),_m(1,255)) – New colour
stars[u].speed=_m(5,15) – New speed
stars[u].rotation=(stars[u].angle*(180/math.pi)) – Set roation to new angle
end
end

end

createStars() – Create the objects
Runtime:addEventListener(“enterFrame”, updateStars) – Update the stars every frame.[/lua]

Hope this helps. [import]uid: 7841 topic_id: 24656 reply_id: 100156[/import]

Assuming your object is pointing to the right with no rotation, you would move it like this:

[lua]local speed = 5 – How fast is object moving

object.x = object.x + math.sin(rotation) * speed
object.y = object.y + math.cos(rotation) * speed[/lua]

If your object starts pointing up before rotating, then your sin and cos functions would need to have (90+rotation) instead of just rotation.

Mark [import]uid: 117098 topic_id: 24656 reply_id: 100540[/import]