newb physics problem : slow-moving object starts on ground, then passes through

I’m starting to work with physics for the first time and have some odd behaviors. I started from the DragPlatforms sample, at least as far as the properties of the objects. But I removed the changing of the bodyType on touch, since I am moving my object via D-pad like controls, from Peach’s tutorial on that.

I start with a couple ground objects which are 60x60 images. I have a blocky ‘car’, a 55x55 image, which starts sitting on one of the ground blocks. I then click on the d-pad controls to slowly move the car over the edge of the ground. When more than half of the car is hanging off the edge, it starts to tip off the edge as you’d expect. But as it begins to fall off the edge it rotates oddly, as if there was something holding it at the upper left corner(when it goes off the right edge of the ground), and then begins to pass through the ground block.

It behaves even stranger when going off the left edge of the ground. Then it actually rotates up and to the left.

I’ve searched the forums, re-read the physics docs, looked at the ‘Solutions to Common Physics Problems’ post; experimented with density, physics.setScale, physics.setPositionIterations, physics.setVelocityIterations(); and adding all the objects to a displayGroup. Nothing has had any effect. I put the physics Draw Mode to debug and didn’t see anything unusual, just the blocky squares.

I’d appreciate any suggestions. Thanks.

Here’s the relevant code:

[lua]local ground = display.newImage( “rockGrid.png” )
ground.x = 60; ground.y = 200
local ground2 = display.newImage( “rockGrid.png” )
ground2.x = 120; ground2.y = 200

physics.addBody( ground, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
physics.addBody( ground2, “static”, { density=3.0, friction=0.5, bounce=0.2 } )

local car= display.newImage( “car55.png” )
car(display.TopLeftReferencePoint)
car.x = 30;
car.y = ground.y - car.height;
physics.addBody( car, { density=1.5, friction=0.5, bounce=0.2 } )

group = display.newGroup()
group:insert( ground )
group:insert( ground2 )
group:insert( car )[/lua] [import]uid: 82378 topic_id: 18544 reply_id: 318544[/import]

It would be helpful if you could provide plug and play code here for testing purposes.

Peach :slight_smile: [import]uid: 52491 topic_id: 18544 reply_id: 71232[/import]

Turns out it was setting the reference point to display.TopLeftReferencePoint that made the strange rotation behavior and I guess, the bodies overlapping as well. But according to this, from the Physics Bodes section of the API Docs :

“Physics assumes the reference point of the object is the center of the object so object:setReferencePoint() may change the reference point from Corona’s Display Object point of view but not for the Physics engine. This affects collisions and how other physics bodies interact.”

That last sentence is confusing. First it seems to say that setting the object’s reference point doesn’t change it for the physics engine, but then it says it does affect collisions and physics bodies interactions. So which is it?

I’m used to positioning my objects from the top left corner. So is this not possible with the physics engine? Looks like no.
[import]uid: 82378 topic_id: 18544 reply_id: 71349[/import]

It may change the reference point for it as a display object but where physics is concerned it should still be the center, is how I would read that.

I’m sorry this isn’t what you are used to; I prefer it myself, although that could be my laziness :wink:

I am glad you found a way to resolve this either way, do you have a suggestion for how that part of the documentation might be clearer? If so I am happy to pass it along.

Peach :slight_smile: [import]uid: 52491 topic_id: 18544 reply_id: 71376[/import]

Here is a plug & playable code that shows that setting the reference point to TopLeft does affect the physics. The way it is now will create the behavior in my initial post. Comment out the car:setReferencePoint line and the behavior changes to what I would expect.

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

display.setStatusBar (display.HiddenStatusBar)
– Hides the status bar

local ground = display.newRect( 60, 170, 60, 60 )
local ground2 = display.newRect( 120, 170, 60, 60 )

physics.addBody( ground, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
physics.addBody( ground2, “static”, { density=3.0, friction=0.5, bounce=0.2 } )

local car = display.newRect( 90, 112.5, 55, 55 )
car:setReferencePoint(display.TopLeftReferencePoint)

physics.addBody( car, { density=1.5, friction=0.5, bounce=0.2 } )

physics.setDrawMode( “hybrid” )

group = display.newGroup()
group:insert( ground )
group:insert( ground2 )
group:insert( car )

– ARROWS –

local up = display.newImage (“upx.png”)
up.x = 70
up.y = 330

local down = display.newImage (“downx.png”)
down.x = 70
down.y = 390

local left = display.newImage (“leftx.png”)
left.x = 30
left.y = 360

local right = display.newImage (“rightx.png”)
right.x = 110
right.y = 360

– Puts in all four movement arrow images and positions them


– MOVE Car –

local motionx = 0
local motiony = 0
local speed = 5
– Speed can be adjusted here to easily change how fast my picture moves. Very convenient!

local function stop (event)
if event.phase == “ended” then
motionx = 0
motiony = 0
end
end

Runtime:addEventListener(“touch”, stop )
– When no arrow is pushed, this will stop me from moving.

local function moveCar (event)
car.x = car.x + motionx
car.y = car.y + motiony
end

Runtime:addEventListener(“enterFrame”, moveCar)
– When an arrow is pushed, this will make me move.

function up:touch(event)
motionx = 0
motiony = -speed
end
up:addEventListener(“touch”, up)

function down:touch()
motionx = 0
motiony = speed
end
down:addEventListener(“touch”, down)

function left:touch()
motionx = -speed
motiony = 0
end
left:addEventListener(“touch”,left)

function right:touch()
motionx = speed
motiony = 0
end
right:addEventListener(“touch”,right)

– The above four functions are stating the arrows should all listen for touches and defining
– the way I should move based on each touch.[/lua] [import]uid: 82378 topic_id: 18544 reply_id: 71701[/import]

This is a wonderful post about cars; I assume that it is going to be very beneficial for the readers since it covers some very important points about car industry.nissan xtrail price | Toyota Corolla For Sale [import]uid: 201494 topic_id: 18544 reply_id: 133622[/import]

This is a wonderful post about cars; I assume that it is going to be very beneficial for the readers since it covers some very important points about car industry.nissan xtrail price | Toyota Corolla For Sale [import]uid: 201494 topic_id: 18544 reply_id: 133622[/import]