Help with Physics: How to make an object move up, left, or right from a given point.

Help with Physics: How to make an object move up, left, or right from a given point. Just wondering how to this this. All I know is if I have an object called rowonecrate1 then If I have this line, physics.addBody( rowonecrate1, { density=3.0, friction=0.5, bounce=0.3 } ). The object will always move down. Not up, left or right. Can someone explain to me how this is done.
[import]uid: 6134 topic_id: 24219 reply_id: 324219[/import]

Hi Romeo,

With physics, gravity is always going to pull your object in one direction. From here you could apply a linear impulse which is like a “push” in the x or y direction.

[lua]rowonecrate1:applyLinearImpulse( 200, 400, rowonecrate1.x, rowonecrate1.y )[/lua]

This would give it a push in both the x and y direction but a bigger push in the y direction.

You could also use transition.to like this

[lua]transition.to(rowonecrate1,{time=1000,y=0})[/lua]

This will move your object from its current y position to a 0 y position over one second.

A third option is translate like this

[lua]rowonecrate1:translate(0,0)[/lua]

This will move your object to 0,0. You could put this code in a loop and move it over time. However, with all of these, if you have gravity affecting your object, it will also pull down on your object while you are moving it.

-Mark
[import]uid: 117098 topic_id: 24219 reply_id: 97815[/import]

Is there a simplier way of doing this ? I have a row of objects I want to be able to move straight up, straight left, or straight right from this point.

[code]

– Bottom Row of blocks
local bottomrowonecrate1 = display.newImage( “crate.png” )
bottomrowonecrate1.x = 30; bottomrowonecrate1.y = 260
bottomrowonecrate1.myName = “bottom row first crate”

local bottomrowonecrate2 = display.newImage( “crate.png” )
bottomrowonecrate2.x = 90; bottomrowonecrate2.y = 260
bottomrowonecrate2.myName = “bottom Row one second crate”

local bottomrowonecrate3 = display.newImage( “crate.png” )
bottomrowonecrate3.x = 150; bottomrowonecrate3.y = 260
bottomrowonecrate3.myName = “bottom Row one third crate”

local bottomrowonecrate4 = display.newImage( “crate.png” )
bottomrowonecrate4.x = 210; bottomrowonecrate4.y =260
bottomrowonecrate4.myName = “bottom Row one fourth crate”

local bottomrowonecrate5 = display.newImage( “crate.png” )
bottomrowonecrate5.x = 270; bottomrowonecrate5.y =260
bottomrowonecrate5.myName = “bottom Row one fourth crate”
physics.addBody( bottomrowonecrate1, “static”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate2, “static”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate3, “static”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate4, “static”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate5, “static”, { density=3.0, friction=0.5, bounce=0.3 } )

[/code] [import]uid: 6134 topic_id: 24219 reply_id: 97983[/import]

Are you saying you want to change the direction of gravity? Or do you still want gravity pulling downward but you just want some force applied to one side of your object? Or maybe you don’t want gravity but you still want your objects to be able to be moved in any direction and still collide with each other? [import]uid: 31262 topic_id: 24219 reply_id: 97987[/import]

I want to change the direction of gravity. Instead of an object failing straight download I want the object to either move straight up, straight left, or straight right from the original position. [import]uid: 6134 topic_id: 24219 reply_id: 98071[/import]

You can change gravity with physics.setGravity(x,y).

The x and y are the amount of gravity in each direction. By default it is (0,9.8) which pulls everything down.

If you wanted it to go up, set it to (0,-9.8)
To go left, set it to (-9.8,0)
To go right, set it to (9.8,0)

Mark
[import]uid: 117098 topic_id: 24219 reply_id: 98158[/import]

my problem is I want to have some items go up, some go left, some go right, and some go down. [import]uid: 6134 topic_id: 24219 reply_id: 98295[/import]

You could try something like this:

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

– Bottom Row of blocks
local bottomrowonecrate1 = display.newImage( “crate.png” )
bottomrowonecrate1.x = 30; bottomrowonecrate1.y = 260
bottomrowonecrate1.myName = “bottom row first crate”

local bottomrowonecrate2 = display.newImage( “crate.png” )
bottomrowonecrate2.x = 90; bottomrowonecrate2.y = 260
bottomrowonecrate2.myName = “bottom Row one second crate”

local bottomrowonecrate3 = display.newImage( “crate.png” )
bottomrowonecrate3.x = 150; bottomrowonecrate3.y = 260
bottomrowonecrate3.myName = “bottom Row one third crate”

local bottomrowonecrate4 = display.newImage( “crate.png” )
bottomrowonecrate4.x = 210; bottomrowonecrate4.y =260
bottomrowonecrate4.myName = “bottom Row one fourth crate”

local bottomrowonecrate5 = display.newImage( “crate.png” )
bottomrowonecrate5.x = 270; bottomrowonecrate5.y =260
bottomrowonecrate5.myName = “bottom Row one fourth crate”

physics.addBody( bottomrowonecrate1, “kinematic”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate2, “kinematic”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate3, “kinematic”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate4, “kinematic”, { density=3.0, friction=0.5, bounce=0.3 } )

physics.addBody( bottomrowonecrate5, “kinematic”, { density=3.0, friction=0.5, bounce=0.3 } )

bottomrowonecrate1:setLinearVelocity(-20,0)
bottomrowonecrate2:setLinearVelocity(0,-20)
bottomrowonecrate4:setLinearVelocity(0,20)
bottomrowonecrate5:setLinearVelocity(20,0)[/lua]

Setting the body types to kinematic wil make them ignore gravity. Then you set a linear velocity on the creates. This code will make four of the crates move in different directions.

Mark [import]uid: 117098 topic_id: 24219 reply_id: 98305[/import]