Move sprites with physic

Hi!

I’m new to corona and try to get the basics to work ;).

How can I move sprites with physics (dynamic)? They should move automatically like in the games lemmings and turn when they hit a barrier.

Any code samples are very welcome ;).

The next step would be to move an physically animated sprite, with joints for the feed and head with physically correct behavior.

Thanks in advance. [import]uid: 74852 topic_id: 12318 reply_id: 312318[/import]

Hello and welcome to the Corona Community.

There are a lot of samples in the Corona SDK folder, but here is a quick physics demo(If this is what you wanted?) . Joints are more complicated, you can read about them here, and also there are sample’s that demonstrate joints in the samples folder of the Corona SDK folder.

[lua]_W = display.contentWidth
_H = display.contentHeight

local physics = require( “physics” )
physics.start()

platform=display.newRect(0, 0, 300, 20 )
platform:setFillColor(255,0,0)
platform.x= _W/2
platform.y=_H/2 + 100;
physics.addBody( platform,“static”, { bounce=0.8, friction=0.1 } )

platform2=display.newRect(0, 0, 20, 200 )
platform2:setFillColor(255,0,0)
platform2.x= _W/2 + 160
platform2.y=_H/2 + 25;
platform2.rotation = 40
physics.addBody( platform2,“static”, { bounce=0.8, friction=0.1 } )

platform3=display.newRect(0, 0, 20, 200 )
platform3:setFillColor(255,0,0)
platform3.x= _W/2 - 160;
platform3.y=_H/2 + 25;
platform3.rotation = -40
physics.addBody( platform3,“static”, { bounce=0.8, friction=0.1 } )

ball = display.newCircle(0, 0, 20);
ball.x = _W * 0.5 - 160;
ball.y = 0;
physics.addBody( ball,“dynamic”, { bounce=0.8, friction=0.1, radius = 20 } )

[import]uid: 17138 topic_id: 12318 reply_id: 44881[/import]

Hi and thanks for the code.

What I’m actually looking for is how to move a pyhsics object, in your code for example the ball. [import]uid: 74852 topic_id: 12318 reply_id: 44937[/import]

There are a few ways to accomplish that. For example, the code below will “throw” the ball up every 5 seconds. Thats just one way of doing it, you can also use applyLinearImpulse, or applyForce. You can find other ways here in the API Reference. [import]uid: 17138 topic_id: 12318 reply_id: 45082[/import]

Ooops, just noticed I forgot to post the code. Sorry.

[lua]_W = display.contentWidth
_H = display.contentHeight

local physics = require( “physics” )
physics.start()

platform=display.newRect(0, 0, 300, 20 )
platform:setFillColor(255,0,0)
platform.x= _W/2
platform.y=_H/2 + 100;
physics.addBody( platform,“static”, { bounce=0.8, friction=0.1 } )

platform2=display.newRect(0, 0, 20, 200 )
platform2:setFillColor(255,0,0)
platform2.x= _W/2 + 160
platform2.y=_H/2 + 25;
platform2.rotation = 40
physics.addBody( platform2,“static”, { bounce=0.8, friction=0.1 } )

platform3=display.newRect(0, 0, 20, 200 )
platform3:setFillColor(255,0,0)
platform3.x= _W/2 - 160;
platform3.y=_H/2 + 25;
platform3.rotation = -40
physics.addBody( platform3,“static”, { bounce=0.8, friction=0.1 } )

ball = display.newCircle(0, 0, 20);
ball.x = _W * 0.5 - 160;
ball.y = 0;
physics.addBody( ball,“dynamic”, {density = 1, bounce=0.8, friction=0.1, radius = 20 } )

local function throw()
ball:applyLinearImpulse(0, -5);
end

timer.performWithDelay(5000, throw, 0); [import]uid: 17138 topic_id: 12318 reply_id: 45491[/import]

Thanks :slight_smile: [import]uid: 74852 topic_id: 12318 reply_id: 45501[/import]