File: test_Car.py

package info (click to toggle)
python-box2d 2.0.2%2Bsvn20100109.244-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,864 kB
  • ctags: 3,280
  • sloc: cpp: 11,679; python: 10,103; xml: 477; makefile: 85; sh: 8
file content (191 lines) | stat: -rw-r--r-- 6,049 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
#
# C++ version Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
# Python version Copyright (c) 2008 kne / sirkne at gmail dot com
# 
# Implemented using the pybox2d SWIG interface for Box2D (pybox2d.googlecode.com)
# 
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.

from test_main import *
class Car (Framework):
    name="Car"
    leftWheel=None
    rightWheel=None
    vehicle=None
    leftJoint=None
    rightJoint=None
    
    _pickle_vars=['leftWheel','rightWheel','vehicle','leftJoint','rightJoint']
    def __init__(self):
        super(Car, self).__init__()
        # car body
        poly1=box2d.b2PolygonDef()
        poly2=box2d.b2PolygonDef()

        # bottom half
        poly1.vertexCount = 5
        poly1.setVertex(4,-2.2,-0.74)
        poly1.setVertex(3,-2.2,0)
        poly1.setVertex(2,1.0,0)
        poly1.setVertex(1,2.2,-0.2)
        poly1.setVertex(0,2.2,-0.74)
        poly1.filter.groupIndex = -1

        poly1.density		= 20.0
        poly1.friction		= 0.68
        poly1.filter.groupIndex	= -1

        # top half
        poly2.vertexCount = 4
        poly2.setVertex(3,-1.7,0)
        poly2.setVertex(2,-1.3,0.7)
        poly2.setVertex(1,0.5,0.74)
        poly2.setVertex(0,1.0,0)
        poly2.filter.groupIndex = -1

        poly2.density		= 5.0
        poly2.friction		= 0.68
        poly2.filter.groupIndex	= -1

        bd=box2d.b2BodyDef() 
        bd.position = (-35.0, 2.8)

        self.vehicle = self.world.CreateBody(bd)
        self.vehicle.CreateShape(poly1)
        self.vehicle.CreateShape(poly2)
        self.vehicle.SetMassFromShapes()

        # vehicle wheels
        circ = box2d.b2CircleDef()
        circ.density = 40.0
        circ.radius = 0.38608
        circ.friction = 0.8
        circ.filter.groupIndex = -1

        bd=box2d.b2BodyDef() 
        bd.allowSleep = False
        bd.position = (-33.8, 2.0)

        self.rightWheel = self.world.CreateBody(bd)
        self.rightWheel.CreateShape(circ)
        self.rightWheel.SetMassFromShapes()

        bd.position = (-36.2, 2.0)
        self.leftWheel = self.world.CreateBody(bd)
        self.leftWheel.CreateShape(circ)
        self.leftWheel.SetMassFromShapes()

        # join wheels to chassis
        jd=box2d.b2RevoluteJointDef() 
        jd.Initialize(self.vehicle, self.leftWheel, self.leftWheel.GetWorldCenter())
        jd.collideConnected = False
        jd.enableMotor = True
        jd.maxMotorTorque = 10.0
        jd.motorSpeed = 0.0
        self.leftJoint = self.world.CreateJoint(jd).getAsType() 

        jd.Initialize(self.vehicle, self.rightWheel, self.rightWheel.GetWorldCenter())
        jd.collideConnected = False
        self.rightJoint = self.world.CreateJoint(jd).getAsType() 

        # ground
        box=box2d.b2PolygonDef() 
        box.SetAsBox(19.5, 0.5)
        box.friction = 0.62

        bd=box2d.b2BodyDef() 
        bd.position = (-25.0, 1.0)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)

        # more ground
        box=box2d.b2PolygonDef() 
        bd=box2d.b2BodyDef() 

        box.SetAsBox(9.5, 0.5, (0,0), 0.1 * box2d.b2_pi)
        box.friction = 0.62
        bd.position = (27.0 - 30.0, 3.1)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)

        # more ground
        box=box2d.b2PolygonDef() 
        bd=box2d.b2BodyDef() 

        box.SetAsBox(9.5, 0.5, (0,0), -0.1 * box2d.b2_pi)
        box.friction = 0.62
        bd.position = (55.0 - 30.0, 3.1)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)

        # more ground
        box=box2d.b2PolygonDef() 
        bd=box2d.b2BodyDef() 

        box.SetAsBox(9.5, 0.5, (0,0), 0.03 * box2d.b2_pi)
        box.friction = 0.62
        bd.position = (41.0, 2.0)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)

        # more ground
        box=box2d.b2PolygonDef() 
        bd=box2d.b2BodyDef() 

        box.SetAsBox(5.0, 0.5, (0,0), 0.15 * box2d.b2_pi)
        box.friction = 0.62
        bd.position = (50.0, 4.0)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)

        # more ground
        box=box2d.b2PolygonDef() 
        bd=box2d.b2BodyDef() 

        box.SetAsBox(20.0, 0.5)
        box.friction = 0.62
        bd.position = (85.0, 2.0)

        ground = self.world.CreateBody(bd) 
        ground.CreateShape(box)
     
    def Step(self, settings):
        self.DrawStringCR("Keys: left = a, brake = s, right = d")
        super(Car, self).Step(settings)
     
    def Keyboard(self, key):
        if not self.leftJoint:
            return

        if key==K_a:
           self.leftJoint.SetMaxMotorTorque(800.0)
           self.leftJoint.SetMotorSpeed(12.0)
           
        elif key==K_s:
           self.leftJoint.SetMaxMotorTorque(100.0)
           self.leftJoint.SetMotorSpeed(0.0)
           
        elif key==K_d:
           self.leftJoint.SetMaxMotorTorque(1200.0)
           self.leftJoint.SetMotorSpeed(-36.0)
             
if __name__=="__main__":
     main(Car)