Physics Sticking

So right now I have a block on top of a platform and when you click him, force is applied to him and he moves left. It works… most of the time. But then sometimes it doesn’t work. I click him and he won’t move at all or something seams to keep him stuck there.

There are other things going on like I have another cube doing the same thing except going up. If you have anymore questions on this, ask me.

**Edit: It’s not actually the collision. I put him in the air and sometimes the gravity in the physics brings him down onto the platform but other times he gets stuck in the air. Almost like it freezes but the other cube that moves can move up. That cube goes through another object which he normally collides with (when the first cube can move).

Here is an example of the code I used:

[code]

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()

local physics = require “physics”
physics.start()
function scene:createScene( event )
local group = self.view

local movementCube = display.newImageRect( “GameImages/movementCube.png”,40, 40 )
movementCube.x, movementCube.y = 390, 163

local function onMoveCubeTap( event )

movementCube.angularVelocity = 100
movementCube:applyForce( -700, 0, movementCube.x, movementCube.y )
end
movementCube:addEventListener( “tap”, onMoveCubeTap )

physics.addBody( movementCube, { density=1.0, friction=0.3, bounce=0.3 } )

local platform = display.newImageRect( “GameImages/platform.png”, 283, 38 )
platform:setReferencePoint( display.CenterReferencePoint )
platform.x, platform.y = 300, 200

local platformShape = { -141,-18, 141,-18, 141,18, -141,18 }
physics.addBody( platform, “static”, { friction=0.3, shape=platformShape } )

group:insert( platform)
group:insert( movementCube)
end

function scene:enterScene( event )
local group = self.view

storyboard.removeScene( “restart” )
storyboard.removeScene( “level1” )
storyboard.removeScene( “menu” )

physics.start()

end

function scene:exitScene( event )
local group = self.view

physics.stop()

end

function scene:destroyScene( event )
local group = self.view

package.loaded[physics] = nil
physics = nil

if restartButton then
restartButton:removeSelf()
restartButton = nil
end
if menuButton then
menuButton:removeSelf()
menuButton = nil
end
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
[/code] [import]uid: 114174 topic_id: 26150 reply_id: 326150[/import]

Make sure you have defined your physics bodies in a clockwise fashion. If you didn’t then your objects bodies will not be how you expect them to be and will result in sticking. [import]uid: 84637 topic_id: 26150 reply_id: 105904[/import]

Do you mean by:

local platformShape = { -141,-18, 141,-18, 141,18, -141,18 }  

I haven’t defined a shape for the movementCube [import]uid: 114174 topic_id: 26150 reply_id: 105907[/import]

Yes, exactly like this. ALWAYS do clockwise coordinate definition. I almost wish that Corona could check this behind the scenes when a polygon body is created, because it’s one of the most frequent causes of “weird physics behavior”, easily preventable but sometimes forgotten.

Brent Sorrentino
Ignis Design [import]uid: 9747 topic_id: 26150 reply_id: 106028[/import]

I’ve just updated my maths library with a function to return true if the points in a polygon wind clockwise:

http://developer.anscamobile.com/code/maths-library

It expects the table to be { {x,y}, … } but can easily be converted to check for { x, y, x, y, … } [import]uid: 8271 topic_id: 26150 reply_id: 106058[/import]

I did define them in clockwise order. I’m going to try to explain this again because I probably did a bad job at doing so before. I’m going to upload it and post a link so you can download it and see exactly what it’s doing.

I pretty much copied level 1 for level 2 to see what it would do. I moved things over in level 2 but it’s the same exact items. Right now, level 1 works completely fine. Physics work, collisions work. Then when I get to level 2, the physics freeze (I put a cube in the air and it won’t fall down. It’s not touching anything) but I can tap on it and it will know. It just won’t see any physics working even though I did the exact same thing I did for level 1.

Get the example here:
http://www.filedropper.com/cubegameexample

[import]uid: 114174 topic_id: 26150 reply_id: 106092[/import]

Hey Mudy

I tested out your app and found that the sticking issue only happens when you use the reload button to reset a level. I had an issue similar to this. You can fix this by adding a slight delay to the restart.lua file when reloading a scene. Swap this bit of code out for what you have in there already:

-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 local group = self.view  
  
 local function reloadLevel()  
 storyboard.gotoScene( storyboard.sceneName, "fade", 500 )  
 end  
 timer.performWithDelay( 200, reloadLevel, 1 )  
  
end  

As you can see all I did was put a 200ms delay before reloading the level. I did this and tested the $hit out of it and can’t get that issue to happen again. I remove the delay and immediately I get the issue again if I use the restart button. Hope that helps. [import]uid: 31262 topic_id: 26150 reply_id: 106128[/import]

Thank you for testing it out! That does fix most of it.

It worked for reloading the level, it never sticks. But it does happen very rarely when I go from Level 1 to Level 2. Then when I hit the menu button and cycle back to level 2, it sometimes works, sometimes sticks.

Is there a way to fix that same thing but without hitting the reload level button? [import]uid: 114174 topic_id: 26150 reply_id: 106152[/import]

Have you tested it after putting in the delay? I found previously that even if you used the reload button on level 1, it would still affect level 2. [import]uid: 31262 topic_id: 26150 reply_id: 106157[/import]

Yeah, I copied the same code you gave me into the restart.lua and it still does it to me.

Like what I’m doing is I’m not reloading any levels at all. I start the game -> play level 1 -> level 2 sometimes sticks/sometimes works -> menu -> play level 1 -> Level 2 sometimes sticks/sometimes works -> menu -> etc.

So it really isn’t hitting the restart.lua at all.

**Edit: I messed with the timer before it transitions from Level 1 to level 2. So far so good. I’ll let you know if it freezes. Thank you guys for taking the time to help me out! :slight_smile: [import]uid: 114174 topic_id: 26150 reply_id: 106165[/import]

It’s freezing now. I haven’t changed any of the code as before except the restart.lua but I’m getting the same freeze like I saw above going from Level 1 to Level 2.

[import]uid: 114174 topic_id: 26150 reply_id: 106217[/import]

Make sure the delay is long enough [import]uid: 31262 topic_id: 26150 reply_id: 106238[/import]

It is and it’s longer than the reload delay. Does it completely work for you?
Though if I take the physics.stop() out under exitScene, it works fine and I’m going to be having physics for all of my levels anyway. Would doing that affect the game later on, like memory wise or is it ok to not stop physics throughout the game? [import]uid: 114174 topic_id: 26150 reply_id: 106240[/import]

NEVER use physics.stop() - use physics.pause()

https://developer.anscamobile.com/reference/index/physicspause

There’s really good reasons for this which were explained when I had a similar issue well over a year ago, but I can’t remember them now.

Just trust me. [import]uid: 8271 topic_id: 26150 reply_id: 106263[/import]

Awesome! Didn’t know that xD. Thank you very much! [import]uid: 114174 topic_id: 26150 reply_id: 106292[/import]