I am making a top down car racing game. I have got the car to work, but when I add a static box for it to collide with, it glitches and does all sorts of unpredicable stuff. Please run the code and diagnose this. I am really confused.
What I am doing here:
- Making a Car, adding a physics body to it
- Making a box, adding a physics body to it
- Adding a listener to move and turn the car via setLinearVelocity & set Rotation
the orange car file is attached below, alternately you could just replace it with a rectangle.
physics = require "physics" physics.start( ) function makeCar() object = display.newImage( "orangecar.png" ) object.rotation = 180 object:scale(.23,.2) object.y = 240 poly = { -20,30,20,30,20,-30,-20,-30 } physics.addBody( object, "dynamic",{shape = poly}) block = display.newRect( 0, 0, 320, 200 ) physics.addBody( block, "static") rotateSpeed = 4; rotateAngle = 0 xChange = 0 yChange = 0 currentRotation = 0 currentRotationRadiant = 0 generalSpeed = 6; SpeedDelta = 0 object:setReferencePoint( display.BottomReferencePoint ) Runtime:addEventListener("enterFrame", move); Runtime:addEventListener("touch", rotate); end function rotate(event) if event.phase == "began" and event.x \< 160 then rotateAngle = rotateSpeed \* -1; end if event.phase == "began" and event.x \> 160 then rotateAngle = rotateSpeed \* 1; end if event.phase == "ended" then rotateAngle = 0 end end function move(inEvent) currentRotation = object.rotation; currentRotationRadiant = (math.pi/180) \* currentRotation; xChange = math.sin(currentRotationRadiant) \* generalSpeed; yChange = math.cos(currentRotationRadiant) \* generalSpeed \* -1; -- object.x = object.x + xChange; -- object.y = object.y + yChange; object:setLinearVelocity( xChange \* 50, yChange \* 50) print(rotateAngle) object:rotate(rotateAngle) end makeCar()