Car Physics simulation

Hi guys,
I have a fairly simple car physics simulation and I’m having issues with the wheels.

The wheels are attached to the box with a pivot joint and between the wheels there is a distance joint.

The car is moved only by gravity.

The problem (as you can see if you execute the code) is that the wheels have an erratic behavior when hitting the bump (there’s a bump on the level, just wait a few seconds) and when hitting the ground back. The wheels are moved outside the joint point and then they return.
I’ve been trying several fixes and combinations without any success.

display.setStatusBar( display.HiddenStatusBar )  
  
require "physics"  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setScale(60)  
  
local main = display.newGroup()  
  
local level = display.newRect(-700, 1800, 10000, 500)  
main:insert(level)  
level:setFillColor(0, 0, 128)  
level.rotation = 20  
physics.addBody(level, "static", {density=1.5, bounce=0, friction=0.2})  
  
bump = display.newRect(2800, 1300, 300, 300)  
main:insert(bump)  
bump:setFillColor(255, 255, 255)  
bump.rotation = -15  
physics.addBody(bump, "static", {density=1.5, bounce=0, friction=0.2})  
  
local box = display.newRect(100, 75, 200, 50)  
main:insert(box)  
box:setFillColor(100,50, 50)  
physics.addBody(box, {density=2, bounce=0.1, friction=0.2})  
  
local frontWheel = display.newCircle(box.x + 45, box.y + 60, 30)  
main:insert(frontWheel)  
physics.addBody(frontWheel, {density=1.5, bounce=0.1, friction=0.1, radius=30})  
  
local rearWheel = display.newCircle(box.x - 45, box.y + 60, 30)  
main:insert(rearWheel)  
physics.addBody(rearWheel, {density=1.5, bounce=0.1, friction=0.1, radius=30})  
  
local frontPivot = physics.newJoint("pivot", frontWheel, box, frontWheel.x, frontWheel.y)  
local rearPivot = physics.newJoint("pivot", rearWheel, box, rearWheel.x, rearWheel.y)  
local dist = physics.newJoint("distance", frontWheel, rearWheel, frontWheel.x, frontWheel.y, rearWheel.x, rearWheel.y)  
  
function update(event)  
 main.x = (box.x \* -1) + display.contentWidth / 2  
 main.y = (box.y \* -1) + display.contentHeight / 2  
end  
  
Runtime:addEventListener("enterFrame", update)  

Any ideas?

Thanks in advance
[import]uid: 13694 topic_id: 16934 reply_id: 316934[/import]

Lower the density of the box to, say, 1. Many people have experienced similar problems, especially when creating ragdolls. There’s a detailed discussion here, although some of it may be out of date by now.

Pivot Joints & Apparent Elasticity [import]uid: 18554 topic_id: 16934 reply_id: 63543[/import]

Hi airhole,
thanks very much for pointing me out to that forum thread… I found there the solution for my problem.

The problem exists when a dynamic body with joints collides with a static one.
I’ve changed the level to dynamic and fixed it to the world with a weld joint and now the wheels works as expected.

It’s a nasty hack but at least I can continue building the game :wink:

Thanks much! [import]uid: 13694 topic_id: 16934 reply_id: 63619[/import]

Would you mind posting the revised code? :slight_smile: [import]uid: 18554 topic_id: 16934 reply_id: 63637[/import]

Sure
Hopefully it will help someone else…

display.setStatusBar( display.HiddenStatusBar )  
   
require "physics"  
physics.start()  
physics.setDrawMode("hybrid")  
physics.setScale(60)  
   
local main = display.newGroup()  
   
local level = display.newRect(-700, 1800, 10000, 500)  
main:insert(level)  
level:setFillColor(0, 0, 128)  
level.rotation = 20  
physics.addBody(level, "dynamic", {density=1.5, bounce=0, friction=0.2})  
   
local levelWeld = display.newRect(-700, 1800, 1, 1)  
main:insert(levelWeld)  
physics.addBody(levelWeld, "static", {density=0, bounce=0, friction=0})  
local levelJoint = physics.newJoint("weld", level, levelWeld, levelWeld.x, levelWeld.y)  
   
bump = display.newRect(2800, 1300, 300, 300)  
main:insert(bump)  
bump:setFillColor(255, 255, 255)  
physics.addBody(bump, "dynamic", {density=1.5, bounce=0, friction=0.2})  
   
local bumpWeld = display.newRect(2800, 1300, 1, 1)  
main:insert(bumpWeld)  
physics.addBody(bumpWeld, "static", {density=0, bounce=0, friction=0})  
local levelJoint = physics.newJoint("weld", bump, bumpWeld, bumpWeld.x, bumpWeld.y)  
   
local box = display.newRect(100, 75, 200, 50)  
main:insert(box)  
box:setFillColor(100,50, 50)  
physics.addBody(box, {density=2, bounce=0.1, friction=0.2})  
   
local frontWheel = display.newCircle(box.x + 45, box.y + 60, 30)  
main:insert(frontWheel)  
physics.addBody(frontWheel, {density=1.5, bounce=0.1, friction=0.1, radius=30})  
   
local rearWheel = display.newCircle(box.x - 45, box.y + 60, 30)  
main:insert(rearWheel)  
physics.addBody(rearWheel, {density=1.5, bounce=0.1, friction=0.1, radius=30})  
   
local frontPivot = physics.newJoint("pivot", frontWheel, box, frontWheel.x, frontWheel.y)  
local rearPivot = physics.newJoint("pivot", rearWheel, box, rearWheel.x, rearWheel.y)  
local dist = physics.newJoint("distance", frontWheel, rearWheel, frontWheel.x, frontWheel.y, rearWheel.x, rearWheel.y)  
   
function update(event)  
 main.x = (box.x \* -1) + display.contentWidth / 2  
 main.y = (box.y \* -1) + display.contentHeight / 2  
end  
   
Runtime:addEventListener("enterFrame", update)  

Cheers [import]uid: 13694 topic_id: 16934 reply_id: 64008[/import]