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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)

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

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 ;)

Thanks much!

Would you mind posting the revised code? :)

views:1436 update:2011/10/28 9:34:19
corona forums © 2003-2011