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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| -- Start physics
local physics = require("physics")
physics.start()
physics.setGravity( 0 , 10 )
-- Set physics drawmode
physics.setDrawMode( "hybrid" )
-- Hide statusbar
display.setStatusBar( display.HiddenStatusBar )
--Determine Device Size
local disw = display.contentWidth
local dish = display.contentHeight
local car = display.newGroup()
--Create The Car
local car_body = display.newRect(0, 0, 102, 20)
car_body:setFillColor(100,75,75)
local rear_weld = display.newRect(14, 10, 8, 8)
local front_weld = display.newRect(84, 10, 8, 8)
rear_weld:setFillColor(100,50, 50)
front_weld:setFillColor(100, 50, 50)
local car_rear_wheel = display.newCircle( rear_weld.x, rear_weld.y, 15 )
local car_front_wheel = display.newCircle( front_weld.x, front_weld.y, 15 )
car_rear_wheel:setFillColor(25,128,25)
car_front_wheel:setFillColor(25,25,200)
--Add An Environment
local lwall = display.newRect(0, 0, 0,dish )
local rwall = display.newRect(disw,0,disw,dish)
local rshape = {0,0,disw,30}
local terrain = display.newRect (-80,200,500,50)
terrain.rotation = 10
local terrainBump = display.newRect (150,196,100,10)
terrainBump.rotation = -5
--Apply The Physics
physics.addBody(lwall,"static", {friction=0.5})
physics.addBody(rwall,"static", {friction=0.5})
physics.addBody(terrain, "static", { density = 1.0, friction = 0.5, bounce = 0.1 })
physics.addBody(terrainBump, "static", { density = 1.0, friction = 0.5, bounce = 0.1 })
physics.addBody(car_body,{density=0,friction=0,bounce=0})
physics.addBody(rear_weld,{density=0,friction=0,bounce=0})
physics.addBody(front_weld,{density=0,friction=0,bounce=0})
physics.addBody(car_rear_wheel,{density=2,friction=3,bounce=0,radius=15})
physics.addBody(car_front_wheel,{density=2,friction=3,bounce=0,radius=15})
-- Create the Joints
local rear_weld_joint = physics.newJoint("weld", rear_weld, car_body, rear_weld.x, rear_weld.y)
local front_weld_joint = physics.newJoint("weld", car_body, front_weld, front_weld.x, front_weld.y)
local rear_wheel_joint = physics.newJoint("pivot",car_rear_wheel,rear_weld,rear_weld.x, rear_weld.y)
local front_wheel_joint = physics.newJoint("pivot",front_weld,car_front_wheel,front_weld.x, front_weld.y)
--Keep The Wheels Separated
local wheel_distance_joint = physics.newJoint("distance",car_rear_wheel, car_front_wheel, car_rear_wheel.x, car_rear_wheel.y, car_front_wheel.x, car_front_wheel.y)
--Populate The Scene
car:insert(lwall)
car:insert(rwall)
car:insert(floor)
car:insert(ramp)
car:insert(car_body)
car:insert(car_rear_wheel)
car:insert(car_front_wheel)
return car |