Physics

Hey everyone, I have some code here that creates a boat and pirate on top of it. The boat rotates randomly as the pirate slips off , everything works pretty fine except for when the boat is rotating the physics of the pirate run very slowly and its not as smooth as I would like it to be. If anyone has any comments or can help me out please do, thanks a lot.

-Caine

[code]

local sprite = require (“sprite”)
local physics = require(“physics”)
physics.start(true)
physics.setGravity(0, 9.81)
physics.setVelocityIterations( 6 )

local H = display.contentHeight
local W = display.contentWidth
local mRand = math.random
----functions-------
local call_start = {}

local gamePlay

local max_r
local trans

set_up = function()
gamePlay = display.newGroup()

local pirate_s = sprite.newSpriteSheet( “pirate.png”, 60, 70 )
local pirate_s2 = sprite.newSpriteSet(pirate_s, 1, 5)
sprite.add( pirate_s2, “still”, 1, 1, 100, 1 )
sprite.add( pirate_s2, “walk”, 2, 3, 300, 0 )
sprite.add( pirate_s2, “jump”, 5, 1, 100, 1 )
pirate = sprite.newSprite( pirate_s2 )
pirate.x, pirate.y = W/2 , H/2
physics.addBody( pirate, “dynamic”, { density=10.5,friction=0.01, bounce=0.0 } )
pirate.isBullet = true
gamePlay:insert(pirate)

boat = display.newImage(“boat.png”,90,200)
physics.addBody( boat, “static”, { density=1.0,friction=0.1, bounce=0.0 } )
gamePlay:insert(boat)
call_start()
end

local function rotate_boat()
max_r = 25
local boatr = mRand(-max_r,max_r)
local boat_rs = mRand(1000, 3000)
trans = transition.to( boat, { time=boat_rs, rotation=(boatr), onComplete=rotate_boat } )
end

call_start = function()
rotate_boat()
end
set_up
[import]uid: 75991 topic_id: 23110 reply_id: 323110[/import]

Could you throw up a zip or plug and play please? Running this would help if possible :slight_smile:

One suggestion to start with though, change this;
physics.setVelocityIterations( 6 )

to

physics.setVelocityIterations( 16 ) [import]uid: 52491 topic_id: 23110 reply_id: 92549[/import]

Sorry to sound like a rookie but this is my first time posting code up here, but i’m not entirely sure how to put up a zip. Also the 6 to 16 velocity iterations didn’t affect it too much. [import]uid: 75991 topic_id: 23110 reply_id: 92663[/import]

You would upload the zip to your server or file hosting site, then post the link here :slight_smile:

One other thing I noticed was how high your pirate’s density is, have you tried playing with that a bit? [import]uid: 52491 topic_id: 23110 reply_id: 92699[/import]

I did try messing around with the friction, bounce, and density. None of them really fixed the problem. Here is the link for the zip file though!
http://www.barbersite.com/Abandonedship.zip [import]uid: 75991 topic_id: 23110 reply_id: 92910[/import]

[lua]physics.addBody( pirate, “dynamic”, { density=5.5,friction=0.01, bounce=0.0 } )

physics.addBody( boat, “static”, { density=6.0,friction=0.1, bounce=0.0 } )[/lua]

Try those settings, it seems to be a bit smoother than previously - I’m not sure quite how “fast” you want the pirate to move on tilt, faster still I would remove friction.

Let me know how that goes :slight_smile: [import]uid: 52491 topic_id: 23110 reply_id: 92948[/import]

It did help a little bit. But the problem persists when the pirate is sliding down on one side, then the boat tilts to the other side and the pirate basically freezes up until the transition is done. I also noticed that if going at a higher velocity the pirate is less likely to freeze up like that. [import]uid: 75991 topic_id: 23110 reply_id: 93143[/import]

At this stage you just have to tweak a bit - if that’s better than what you had then try something else; which is better? Try something closer to that, etc.

For the issue there, can you try setting his linear velocity to 0,0 when the boat tilts again? That may help with the “freezing”. [import]uid: 52491 topic_id: 23110 reply_id: 93220[/import]