Physics not playing nicely? (plug n play code included)

Ok so about 15 minutes ago, I was looking for back and forth movement of object, ok I found that here

http://developer.anscamobile.com/forum/2011/07/07/block-movement-back-and-forth This was really cool.
So then I took that code from above, added a ball and rotated the platform and *tried* to get it to act proper.

Go ahead and fire this up and you’ll see what I mean. I made sure to use generated shapes so you can just copy paste and launch.

…Assuming you have launched it by now :slight_smile:

Ok, see how the circle just kind of sits there and then flies off suddenly or slowly slides down, or just acts WEIRD?

How do I prevent that weird behavior? I want it to GENTLY roll and act like a ball would in real life on an angled surface and rotate and react like it should.I tried isBullet I am not sure what I am doing. I do mean I don’t know what I am doing :slight_smile:

I started with corona 2 months ago, I have a game put together and now I am adding rotating platforms, objects etc but I am stuck due to this weird behavior when round objects encounter rotating, rotated or anything moving.

Thanks for help in advance!

-Nick G.

[code]

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

–Object back and forth animation

–mplat = moving platform
local mplat = display.newRect( 0, 0, 250, 50)
mplat.x = 200
mplat.y = 200
mplat.rotation = 40
mplat.isBullet = true

physics.addBody( mplat, “static”, { density=1.0, friction=1.0, bounce=0 })

local circle = display.newCircle (150,0,50,50)
physics.addBody( circle, { density=2.9, friction=1.0, bounce=0.7, radius=45 } )
circle.isBullet = true
–changing time will dictate how fast the object will go left on return
function goLeft ()
transition.to( mplat, { time=1500, x=(mplat.x - 150), onComplete=goRight } )
end

– changing time will dictate how fast the object goes right on return
function goRight ()
transition.to( mplat, { time=1500, x=(mplat.x + 150), onComplete=goLeft } )
end

goLeft()

[code] [import]uid: 61600 topic_id: 13012 reply_id: 313012[/import]

Your moving platform needs to be dynamic. I would remove the collision from your current mover and attach a dynamic object with weld joints to the static object.
[import]uid: 7177 topic_id: 13012 reply_id: 47778[/import]

Weld joints?

Sheesh, no idea how to do that :expressionless:

I just made the platform dynamic, but as I expected it just falls haha.

Weld joints…I’ll have to read about it.

Do you think you could show me an example code? :slight_smile: :slight_smile:

-Nick G [import]uid: 61600 topic_id: 13012 reply_id: 47780[/import]

[blockcode]
local physics = require (“physics”)
physics.start()
physics.setDrawMode( “hybrid” )

–Object back and forth animation

–splat = static moving platform
local splat = display.newRect( 0, 0, 30, 30)
splat.x = 200
splat.y = 200
splat.rotation = 0
physics.addBody( splat, “static”, { density=1.0, friction=1.0, bounce=0 })

–dplat = dynamic platform
local dplat = display.newRect( 0, 0, 250, 50)
dplat.x = 200
dplat.y = 200
dplat.rotation = 40
physics.addBody( dplat, “dynamic”, { density=1.0, friction=1.0, bounce=0 })

– weld object togeather with a weld joint
physics.newJoint( “weld”, splat, dplat, splat.x ,splat.y )

local circle = display.newCircle (150,0,50,50)
physics.addBody( circle, { density=2.9, friction=1.0, bounce=0.7, radius=45 } )
circle.isBullet = true

–changing time will dictate how fast the object will go left on return
function goLeft ()
transition.to( splat, { time=1500, x=(splat.x - 150), onComplete=goRight } )
end

– changing time will dictate how fast the object goes right on return
function goRight ()
transition.to( splat, { time=1500, x=(splat.x + 150), onComplete=goLeft } )
end

goLeft()
[import]uid: 7177 topic_id: 13012 reply_id: 47781[/import]

@Darkmod.
Wow man you are QUICK. That’s exactly what did it! That was seriously some cool stuff man.

Could you shed some light as to why that is? Why doesn’t an object have proper interaction without all this fancy welding joints?

I looked at the API for weld and all the other type of joints.

All I can say is DarkMod rocks!

You got a website or something I can donate to? I learned a lot from this and realize I got a long long way to go to really understand things since I have been programming for all of 6 weeks or so in lua and never anything before that…yea

-Nick G. [import]uid: 61600 topic_id: 13012 reply_id: 47807[/import]

Thx Nick.

Donations are not necessary but but thx. I don’t have a public web site right now. but feel free to hit me up with questions.

It all comes down to the Box2d and its limitations.

Basic rules I use with 2d box.

  1. Any moving or movable object needs to be “dynamic” or it will not react correctly in the physics engine.
    a. Movers are attached to moving static objects using joints.
    b. Touch to move i use newJoint(“touch”, obj… check out the rope demo for touch joints http://developer.anscamobile.com/code/rope-demo

Heres a good Box2d demo: http://box2dflash.sourceforge.net/
-Darkmod
[import]uid: 7177 topic_id: 13012 reply_id: 47812[/import]

That is some very good information. I didn’t know about dynamic, I mean I knew about what it was but not how things interact.

I know box2d has math limitations in rounding numbers, as I was trying to have an object bounce and be exact in height each time, only to find out it varies +/- a few pixels due to number rounding. This is just one of those weird things I guess I’ll have to comb through and discover :slight_smile:

Thanks for the information, it was very very helpful.

I’ll keep those “rules” in mind :slight_smile:

-Nick G [import]uid: 61600 topic_id: 13012 reply_id: 47815[/import]

@nicholasclayg

the code you had posted is perfect you need to change only one line

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

–Object back and forth animation

–mplat = moving platform
local mplat = display.newRect( 0, 0, 250, 50)
mplat.x = 200
mplat.y = 200
mplat.rotation = 40
mplat.isBullet = true

physics.addBody( mplat, “kinematic”, { density=1.0, friction=1.0, bounce=0 })

local circle = display.newCircle (150,0,50,50)
physics.addBody( circle, { density=2.9, friction=1.0, bounce=0.7, radius=45 } )
circle.isBullet = true

–changing time will dictate how fast the object will go left on return
function goLeft ()
transition.to( mplat, { time=1500, x=(mplat.x - 150), onComplete=goRight } )
end

– changing time will dictate how fast the object goes right on return
function goRight ()
transition.to( mplat, { time=1500, x=(mplat.x + 150), onComplete=goLeft } )
end

goLeft() [/lua]
see the difference at line 14 [import]uid: 12482 topic_id: 13012 reply_id: 47829[/import]

@hgvyas123

That works too. Now the question is which method is more efficient, or best?

I had a little fun and started animating things (rotating ramp)

What I am finding is the collision detection lags a bit when things go fast. In this example, the circle kind of absorbs a bit into the rotating platform.

Darkmod,

Is there a way to update the collision detection to be more accurate (I know on tilt controls, you can up the interval for checking)? I thought I read somewhere it was like 3 or something and you can do something higher at the cost of performance?

Here is my updated fun silly code with tilt controls:)

[code]
local physics = require (“physics”)
physics.start()
physics.setDrawMode( “hybrid” )

physics.setScale( 60 )
physics.setGravity( 0, 10 ) – initial gravity points downwards

–Object back and forth animation

–splat = static moving platform
–In hybrid draw mode this is the little square
–This square will be used to weld to the platform we want to move/animate
–Essentially we control this square to manipulate the object welded to it.
local splat = display.newRect( 0, 0, 30, 30)
splat.x = 200
splat.y = 200
splat.rotation = 0
physics.addBody( splat, “static”, { density=1.0, friction=5.0, bounce=0 })

–dplat = dynamic platform
local dplat = display.newRect( 0, 0, 250, 50)
dplat.x = 200
dplat.y = 200
dplat.rotation = 0
physics.addBody( dplat, “dynamic”, { density=1.0, friction=5.0, bounce=0 })
–I define a local rotateAmount = Number, a baseline factor

–under mybody.rotation = mybody.rotation + or - number (I use this for fine
–tuning the animation…

local rotateAmount = 1
local time = system.getTimer()

local function animate( event )
splat.rotation = splat.rotation +5.5
local now = system.getTimer()
local elapsed = now - time
local rotationAngle = rotateAmount * (elapsed / 1000.0) * 10
time = now
splat.rotation = splat.rotation + rotationAngle
end

Runtime:addEventListener( “enterFrame”, animate );

– weld object togeather with a weld joint
physics.newJoint( “weld”, splat, dplat, splat.x ,splat.y )

local circle = display.newCircle (90,0,50,50)
physics.addBody( circle, { density=2.9, friction=5.0, bounce=0.3, radius=49 } )
circle.isBullet = true

–changing time will dictate how fast the object will go left on return
function goLeft ()
transition.to( splat, { time=1500, x=(splat.x - 150), onComplete=goRight } )
end

– changing time will dictate how fast the object goes right on return
function goRight ()
transition.to( splat, { time=1500, x=(splat.x + 150), onComplete=goLeft } )
end

goLeft()

—TILT CONTROLS BASED ON GRAVITY (AFFECTS EVERYTIHING
–This tilt ONLY allows left and right control of an object,
–in this case a CIRLCE :slight_smile: Cuz its happy time!!!

system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness

function onTilt( event )
–physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 * event.yGravity ) )
physics.setGravity( ( 9.8 * event.xGravity ), ( 9.8) )
end
Runtime:addEventListener( “accelerometer”, onTilt )
[/code] [import]uid: 61600 topic_id: 13012 reply_id: 47832[/import]

this will help

physics.setPositionIterations( 32 )
default is 8 put it after physics.start() more higher number more accurate and more slow down performance

:slight_smile: [import]uid: 12482 topic_id: 13012 reply_id: 47834[/import]

@hgvyas123

Yes! That was exactly what I was looking for. I had read that somewhere and saved it…somewhere.

That fixed it.

Ok so here is a more advanced question (I’m learning alot this last couple days just from this thread)
Since this is a performance hit physics.setPositionIterations( 32 )

Is there a way to selectively apply it to just the objects I need it on?

The reason I ask is when I run the rest of my test level, everything reacts fine which got me thinking “Optimization”. So could I um, I don’t know the term but “nest” (I think?) the physics Iterations to only apply to things that need it?

I noticed it’s anything that moves faster than 1:2 seems to have problems (referring to the previously pasted code on line 35 and 39) Right now it’s set to 1:5.5

I was thinking ok, maybe I could just apply the iterations to the Circle only so that it’s always updating OR/and I could also do this with moving/rotating objects, platforms etc.

I don’t know what would be a good practice.

So is it possible to nest/bury that physics.setPositionIterations to specific objects?

Thanks for the help, this is freaking awesome. I feel like I am living my childhood (I’m 35) all over again and making games! I am sure my wife thinks I am nuts by now hehe… w00t!

-Ng

[import]uid: 61600 topic_id: 13012 reply_id: 47879[/import]

few other setting you might want to play around with

physics.setScale( 32 )
physics.setVelocityIterations( 32 )
physics.setPositionIterations( 3 )
– config.lua

fps = 30, your fps will change the speed and feel of the game to some degree

[import]uid: 7177 topic_id: 13012 reply_id: 47907[/import]

as darkmod said set fps = 30 in config if you had set it to 60 i had note this when there are more objects or more calculation fps = 60 is decreasing speed of course it is giving very extra load on device

and physics.setPositionIterations is the global you can not set it differently for each object. if 32 is too much decreasing performance set it some what down say 24,16 it is ok as first requirement in each game is smoothness (good performance)

:slight_smile: [import]uid: 12482 topic_id: 13012 reply_id: 47935[/import]

Ok, I think I have this issue sorted out.

I started a new topic on rotation of map and physics, this is my new challenge lol.
Thank you hgvyas123 and Darkmod - I really learned alot!

Now, here is my new issue: :slight_smile: maybe you guys know something?

http://developer.anscamobile.com/forum/2011/07/30/rotate-physics-mapobject-issue-plug-and-play-code-included [import]uid: 61600 topic_id: 13012 reply_id: 48003[/import]