Moving object

Hi all this is my first adventure into the world of corona and lua and I have to say between the tutorials and these forums it is not that hard, so many thanks.

I have just a quick question though. I have an object and I can rotate it with a joystick, but I also want to move it in the direction that it is turned

Does anybody know any good tutorials rf suggest what math I may need to achieve this???

The joystick tutorial is very good but it only moves the background under the object.

Many thanks in advance
Chris [import]uid: 10697 topic_id: 3212 reply_id: 303212[/import]

Take a look at the physics force/impulse methods:

http://developer.anscamobile.com/content/game-edition-physics-bodies#body:applyForce

You shouldn’t need much math. [import]uid: 8353 topic_id: 3212 reply_id: 9692[/import]

Hey Chris

Did you mean my joystick http://developer.anscamobile.com/code/joystick as the plane moves independently from the background in that one.

The physics force / impulse methods may be of use but are not suitable for most games. Using physics is overkill for many types of game where you just need to alter the x and y values of the character.

You can do this with a dpad by just taking the x and y values of the dpad and adding them to the character. So if the dpad is pressed up then I would minus 1 from the y every frame. Thats a bit fast so I might minus 1/30 from the y each frame. Depends on the speed you want.

If an analogue joystick / moving the character in any direction, you still need the x and y values but these need to be altered for the direction the character is traveling. So you have a force / vector and an angle you then calculate the x and y using cos / sin.

Third way is with physics, using force and angular force to turn. This has its own limitations and when applying the force you would still need to use sin / cos to calculate the angle of the force in relation to your character. [import]uid: 5354 topic_id: 3212 reply_id: 9697[/import]

Thanks so much for your advice I am now going to look into them, I got it moving around the screen using the following code

local function moveBull( joyY )
if joyY and joyY ~= false then
bull.y = bull.y + ( joyY * 2 )
end
end

local function sideBull( joyX )
if joyX and joyX ~= false then
bull.x = bull.x + ( joyX * 2 )
end
end

so may now try to find a way to rotate the object in the way of travel

any ideas???

once again so many thanks
Chris [import]uid: 10697 topic_id: 3212 reply_id: 9744[/import]

If its my joystick it also sends back an angle, joyAngle, that will tell you where the stick is pointing and thats usually the direction of the object (you may need to flip 180 degress / +180 ) but try that. [import]uid: 5354 topic_id: 3212 reply_id: 9746[/import]

Heres some code for a physics version, the joystick shouldn’t really be used here, if I was doing it properly I would have 2 buttons, a left and a right to replace the joystick.

function movement ()  
 local rotation = joystick.joyAngle  
 local vector = joystick.joyVector  
 if rotation ~= false and vector ~= false then  
 if rotation \> 0 and rotation \<= 180 then  
 vector = vector / 10  
 YOUROBJECT:applyTorque( vector )  
 elseif rotation \> 180 and rotation \<= 360 then  
 vector = -vector / 10  
 YOUROBJECT:applyTorque( vector )  
 end  
 end  
  
end  
Runtime:addEventListener( "enterFrame", movement)  

This bit will apply an acceleration to your object and should be used in combination with an accelerator type button

function moveTarget( ) local angle = YOUROBJECT.rotation local sin = math.sin local cos = math.cos local pi = math.pi local force = 0.7 local xFinal = ( sin( angle \* pi / 180 ) \* force ) local yFinal = ( cos( angle \* pi / 180 ) \* force ) YOUROBJECT:applyForce( xFinal , -yFinal, YOUROBJECT.x, YOUROBJECT.y ) end [import]uid: 5354 topic_id: 3212 reply_id: 9749[/import]

Could you post a sample we can download and play with?
Might be beneficial to all that are looking as well.

Thanks for sharing and being so helpful. [import]uid: 7856 topic_id: 3212 reply_id: 9754[/import]

wow matthew you are a star!!!

but I am now going to learn a bit more about lua, and have a think about using buttons and not a joystick, I played around a bit and got it to roate but it did not stop

local function moveBull( joyY )
if joyY and joyY ~= false then
bull.y = bull.y + ( joyY * 2 )
end
end

local function sideBull( joyX )
if joyX and joyX ~= false then
bull.x = bull.x + ( joyX * 2 )
end
end

local function rotateBull( joyAngle )
if joyAngle and joyAngle ~= false then
bull.rotation = bull.rotation + ( joyAngle )
end
end
[import]uid: 10697 topic_id: 3212 reply_id: 9756[/import]

Now I have a question,

would it be possible for 2 joysticks just on the Y axis, each one controls a different side of an object, i.e a tanks controls???

not sure how easy this would be [import]uid: 10697 topic_id: 3212 reply_id: 9758[/import]

Right guys here is a simple demo using physics

http://www.alienhorde.com/files/movement.zip

You are moving a spaceship ( in space ). You can rotate the ship left and right using the joystick and give the ship thrust with the other button.

It will glide to a stop after a while ( not like in real space ).

Im also adding a camera to the scene and even though the ship is actually moving it stays in the centre of the iPhone screen. Its setup for normal iPhone resolution.

So you can do rolling balls, motor boats, tanks, spaceships, all sorts with this principle. [import]uid: 5354 topic_id: 3212 reply_id: 9759[/import]

There is a sample in the joystick folder with a 2 joystick setup, and its got a tank too!

Expand on that by applying the force at different points on the tank and it should rotate like a tank [import]uid: 5354 topic_id: 3212 reply_id: 9761[/import]