Hi All,
I am not able to put together a working car using physics and ‘wheel joints’.
the ‘billy car’ example does not work and I haven’t found another tutorial only more posts seeking for help…
Here’s my code. It’s straight forward and mostly works but: I can’t set the motorSpeed other than 1 or 0. Is it meant to be used like that or do I have to use applyForce somehow?
Please, somebody with experience with box2d have a look here. It’s only a few loc:
local M = {} local wh = display.viewableContentWidth \*0.5 local hh = display.viewableContentHeight \*0.5 local cs = {x=wh, y=hh-200} local rk = {{x=cs.x+200, y=cs.y+250}, {x=cs.x-200, y=cs.y+250} } function M.new(par) local ph = require( "physics" ) local group = display.newGroup() physics.setPositionIterations( 16 ) physics.setVelocityIterations( 6 ) physics.setDrawMode( "hybrid" ) -- physics.setDrawMode( "debug" ) ph.start() -- physics.setContinuous( true ) physics.setGravity( 0, 20 ) local body = display.newRect( group, cs.x, cs.y, 300, 300 ) ph.addBody( body, 'dynamic', {density=2.0, friction=0.3, bounce=0.2} ) body.rad = {} body.aufh = {} for i=1,2 do body.rad[i] = display.newCircle( group, rk[i].x, rk[i].y, 150 ) ph.addBody( body.rad[i], 'dynamic', {density=1, friction=1.3, bounce=0.8, radius= 150} ) body.aufh[i]= ph.newJoint( 'wheel', body, body.rad[i], rk[i].x, rk[i].y, 0, 30) end -- rear wheel is motorized body.aufh[2].isMotorEnabled = true body.aufh[2].maxMotorTorque = 10000000 local boden = display.newRect( group, \_w/2, \_h-30, 1000, 10 ) ph.addBody( boden, 'static', {density=1.0, friction=1.3, bounce=0.2} ) boden.alpha = 1 local kiste = display.newRect( group, 900, \_h-80, 50, 50 ) ph.addBody( kiste, 'dynamic', {density=1.0, friction=5.3, bounce=0.2} ) boden.alpha = 1 local btn = display.newRect( group, \_w-100, 100, 100, 100) btn:addEventListener('touch', function() print( body.aufh[2].motorSpeed, body.aufh[2].motorTorque ) -- wtf? no matter what I set the speed to, it's stuck at 1, which is slow body.aufh[2].motorSpeed = body.aufh[2].motorSpeed + 0.01 -- body.aufh[2].motorSpeed = 0.3 -- body.aufh[2].motorSpeed = -5000 end ) Runtime:addEventListener('enterFrame', function() print( body.aufh[2].motorSpeed ) end ) return group end return M