Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/elements/elements.py
blob: 4d9a3614d8993caca9e2b2c983d02d6eaac8c5d0 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/python
"""
This file is part of the 'Elements' Project
Elements is a 2D Physics API for Python (supporting pybox2d)

Copyright (C) 2008, The Elements Team, <elements@linuxuser.at>

Home:  http://elements.linuxuser.at
IRC:   #elements on irc.freenode.org

Code:  http://www.assembla.com/wiki/show/elements
       svn co http://svn2.assembla.com/svn/elements                     

License:  GPLv3 | See LICENSE for the full text
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.              
"""
__version__=  '0.11'
__contact__ = '<elements@linuxuser.at>'

# Load Box2D
try:
    import Box2D as box2d
except:
    print 'Could not load the pybox2d library (Box2D).'
    print 'Please run "setup.py install" to install the dependencies.'
    print 
    print 'Alternatively, recompile pybox2d for your system and python version.'
    print "See http://code.google.com/p/pybox2d"
    exit()

# Standard Imports
from random import shuffle

# Load Elements Definitions
from locals import *

# Load Elements Modules
import tools
import drawing
import add_objects
import callbacks
import camera

# Main Class
class Elements:
    """The class which handles all interaction with the box2d engine
    """
    # Settings
    run_physics   =True           # Can pause the simulation
    element_count =0              # Element Count 
    renderer      =None           # Drawing class (from drawing.py)
    input         =INPUT_PIXELS   # Default Input in Pixels! (can change to INPUT_METERS)
    line_width    =0              # Line Width in Pixels (0 for fill)
    listener      =None
    
    screen_offset = (0, 0)        # Offset screen from world coordinate system (x, y) [meter5]
    screen_offset_pixel = (0, 0)  # Offset screen from world coordinate system (x, y) [pixel]
    
    # The internal coordination system is y+=up, x+=right
    # But it's possible to change the input coords to something else,
    # they will then be translated on input
    inputAxis_x_left = False    # positive to the right by default
    inputAxis_y_down = True     # positive to up by default

    mouseJoint = None

    def __init__(self, screen_size, gravity=(0.0,-9.0), ppm=100.0, renderer='pygame'):
        """ Init the world with boundaries and gravity, and init colors.
        
            Parameters:
              screen_size .. (w, h) -- screen size in pixels [int]
              gravity ...... (x, y) in m/s^2  [float] default: (0.0, -9.0)
              ppm .......... pixels per meter [float] default: 100.0
              renderer ..... which drawing method to use (str) default: 'pygame'

            Return: class Elements()
        """
        self.set_screenSize(screen_size)
        self.set_drawingMethod(renderer)
        
        # Create Subclasses
        self.add = add_objects.Add(self)
        self.callbacks = callbacks.CallbackHandler(self)
        self.camera = camera.Camera(self)
        
        # Set Boundaries
        self.worldAABB=box2d.b2AABB()
        self.worldAABB.lowerBound = (-100.0, -100.0)
        self.worldAABB.upperBound = (100.0, 100.0)
        
        # Gravity + Bodies will sleep on outside
        self.gravity = gravity
        self.doSleep = True
    
        # Create the World
        self.world = box2d.b2World(self.worldAABB, self.gravity, self.doSleep)

        # Init Colors        
        self.init_colors()
        
        # Set Pixels per Meter
        self.ppm = ppm

    def set_inputUnit(self, input):
        """ Change the input unit to either meter or pixels
        
            Parameters:
              input ... INPUT_METERS or INPUT_PIXELS
          
            Return: -
        """
        self.input = input
        
    def set_inputAxisOrigin(self, left=True, top=False):
        """ Change the origin of the input coordinate system axis
        
            Parameters:
              left ... True or False -- x = 0 is at the left?
              top .... True or False -- y = 0 is at the top?
          
            Return: -
        """          
        self.inputAxis_x_left = not left
        self.inputAxis_y_down = top

    def set_drawingMethod(self, m, *kw):
        """ Set a drawing method (from drawing.py)
        
            Parameters:
              m .... 'pygame' or 'cairo'
              *kw .. keywords to pass to the initializer of the drawing method

            Return: True if ok, False if no method identifier m found
        """
        try:
            self.renderer = getattr(drawing, "draw_%s" % m) (*kw)
            return True
        except AttributeError:
            return False
            
    def set_screenSize(self, size):
        """ Set the current screen size
        
            Parameters: 
              size ... (int(width), int(height)) in pixels
              
            Return: -
        """
        self.display_width, self.display_height = size

    def init_colors(self):
        """ Init self.colors with a fix set of hex colors
        
            Return: -        
        """
        self.fixed_color = None
        self.cur_color = 0
        self.colors = [
          "#737934", "#729a55", "#040404", "#1d4e29", "#ae5004", "#615c57",
          "#6795ce", "#203d61", "#8f932b"
        ]
        shuffle(self.colors)

    def set_color(self, clr):
        """ Set a fixed color for all future Elements (until reset_color() is called) 
        
            Parameters: 
              clr ... Hex '#123123' or RGB ((r), (g), (b))
              
            Return: -
        """
        self.fixed_color = clr
    
    def reset_color(self):
        """ All Elements from now on will be drawn in random colors
           
            Return: - 
        """
        self.fixed_color = None

    def get_color(self):
        """ Get a color - either the fixed one or the next from self.colors 
        
            Return: clr = ((R), (G), (B)) 
        """
        if self.fixed_color != None:
            return self.fixed_color
            
        if self.cur_color == len(self.colors): 
            self.cur_color = 0
            shuffle(self.colors)
    
        clr = self.colors[self.cur_color]
        if clr[0] == "#":
            clr = tools.hex2rgb(clr)
        
        self.cur_color += 1
        return clr
    
    def update(self, fps=50.0, vel_iterations=10, pos_iterations=8):
        """ Update the physics, if not paused (self.run_physics)
        
            Parameters:
              fps ............. fps with which the physics engine shall work
              vel_iterations .. velocity substeps per step for smoother simulation
              pos_iterations .. position substeps per step for smoother simulation
            
            Return: -
        """
        if self.run_physics:
            self.world.Step(1.0 / fps, vel_iterations, pos_iterations)

    def translate_coord(self, point):
        """ Flips the coordinates in another coordinate system orientation, if necessary
            (screen <> world coordinate system) 
        """
        x, y = point

        if self.inputAxis_x_left:
            x = self.display_width - x

        if self.inputAxis_y_down:
            y = self.display_height - y
            
        return (x, y)
        
    def translate_coords(self, pointlist):
        """ Flips the coordinates in another coordinate system orientation, if necessary 
            (screen <> world coordinate system) 
        """    
        p_out = []        
        for p in pointlist:
            p_out.append(self.translate_coord(p))
        return p_out

    def to_world(self, pos):
        """ Transfers a coordinate from the screen to the world coordinate system (pixels)
            - Change to the right axis orientation
            - Include the offset: screen -- world coordinate system
            - Include the scale factor (Screen coordinate system might have a scale factor)
        """
        dx, dy = self.screen_offset_pixel
        
        x = pos[0] / self.camera.scale_factor
        y = pos[1] / self.camera.scale_factor
        
        x, y = self.translate_coord((round(x), round(y)))
        return (x+dx, y+dy) 
        
    def to_screen(self, pos):
        """ Transfers a coordinate from the world to the screen coordinate system (pixels)
            and by the screen offset
        """
        dx, dy = self.screen_offset_pixel
        x = pos[0] - dx
        y = pos[1] - dy
        
        sx, sy = self.translate_coord((x, y))
        return (sx * self.camera.scale_factor, sy * self.camera.scale_factor)
                         
    def meter_to_screen(self, i):
        return i * self.ppm * self.camera.scale_factor
        
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm
        sy /= self.ppm

        f = area/self.camera.scale_factor

        AABB=box2d.b2AABB()
        AABB.lowerBound = (sx-f, sy-f)
        AABB.upperBound = (sx+f, sy+f)

        amount, shapes = self.world.Query(AABB, 2)

        if amount == 0:
            return False
        else:
            bodylist = []
            for s in shapes:
                body = s.GetBody()
                if not include_static:
                    if body.IsStatic() or body.GetMass() == 0.0:
                        continue
                        
                if s.TestPoint(body.GetXForm(), (sx, sy)):
                    bodylist.append(body)

            return bodylist
    
    def draw(self):
        """ If a drawing method is specified, this function passes the objects
            to the module in pixels.
            
            Return: True if the objects were successfully drawn
              False if the renderer was not set or another error occurred
        """
        self.callbacks.start(CALLBACK_DRAWING_START)
        
        # No need to run through the loop if there's no way to draw        
        if not self.renderer: 
            return False

        if self.camera.track_body:
            # Get Body Center
            p1 = self.camera.track_body.GetWorldCenter()   
            
            # Center the Camera There, False = Don't stop the tracking
            self.camera.center(self.to_screen((p1.x*self.ppm, p1.y*self.ppm)), stopTrack=False) 
            
        # Walk through all known elements
        self.renderer.start_drawing()
        
        for body in self.world.bodyList:
            xform = body.GetXForm()
            shape = body.GetShapeList()
            angle = body.GetAngle()
            
            if shape:
                userdata = body.GetUserData()
                clr = userdata['color']
                
            for shape in body.shapeList:                                
                type = shape.GetType()
                                
                if type == box2d.e_circleShape:
                    position = box2d.b2Mul(xform, shape.GetLocalPosition())
                    
                    pos = self.to_screen((position.x*self.ppm, position.y*self.ppm))                    
                    self.renderer.draw_circle(clr, pos, self.meter_to_screen(shape.radius), angle)

                elif type == box2d.e_polygonShape:
                    points = []
                    for v in shape.vertices:
                        pt = box2d.b2Mul(xform, v)
                        x, y = self.to_screen((pt.x*self.ppm, pt.y*self.ppm))
                        points.append([x, y])

                    self.renderer.draw_polygon(clr, points)
                   
                else:
                    print "  unknown shape type:%d" % shape.GetType()
    

        for joint in self.world.jointList:
            p2 = joint.GetAnchor1()
            p2 = self.to_screen((p2.x*self.ppm, p2.y*self.ppm))
            
            p1 = joint.GetAnchor2()
            p1 = self.to_screen((p1.x*self.ppm, p1.y*self.ppm))
            
            if p1 == p2:
                self.renderer.draw_circle((255,255,255), p1, 2, 0)
            else:
                self.renderer.draw_lines((0,0,0), False, [p1, p2], 3)

        self.callbacks.start(CALLBACK_DRAWING_END)
        self.renderer.after_drawing()
        
        return True


    def mouse_move(self, pos):
        pos = self.to_world(pos)
        x, y = pos
        x /= self.ppm
        y /= self.ppm
                        
        if self.mouseJoint:
            self.mouseJoint.SetTarget((x,y))

    def pickle_save(self, fn, additional_vars={}):
        import cPickle as pickle
        self.add.remove_mouseJoint()
    
        if not additional_vars and hasattr(self, '_pickle_vars'):
            additional_vars=dict((var, getattr(self, var)) for var in self._pickle_vars)

        save_values = [self.world, box2d.pickle_fix(self.world, additional_vars, 'save')]

        try:
            pickle.dump(save_values, open(fn, 'wb'))
        except Exception, s:
            print 'Pickling failed: ', s
            return

        print 'Saved to %s' % fn

    def pickle_load(self, fn, set_vars=True, additional_vars=[]):
        """
        Load the pickled world in file fn.
        additional_vars is a dictionary to be populated with the
        loaded variables.
        """
        import cPickle as pickle
        try:
            world, variables = pickle.load(open(fn, 'rb'))
            world = world._pickle_finalize()
            variables  = box2d.pickle_fix(world, variables, 'load')
        except Exception, s:
            print 'Error while loading world: ', s
            return
        
        self.world = world
        
        if set_vars:
            # reset the additional saved variables:
            for var, value in variables.items():
                if hasattr(self, var):
                    setattr(self, var, value)
                else:
                    print 'Unknown property %s=%s' % (var, value)

        print 'Loaded from %s' % fn

        return variables

    def json_save(self, path, additional_vars = {}, serialize=False):
        import json
        worldmodel = {}

        save_id_index = 1
        self.world.GetGroundBody().userData = {"saveid" : 0}

        bodylist = []
        for body in self.world.GetBodyList():
            if not body == self.world.GetGroundBody():
                body.userData["saveid"] = save_id_index #set temporary data
                save_id_index+=1
                shapelist = body.GetShapeList()
                modelbody = {}
                modelbody['position'] = body.position.tuple()
                modelbody['dynamic'] = body.IsDynamic()
                modelbody['userData'] = body.userData
                modelbody['angle'] = body.angle
                modelbody['angularVelocity'] = body.angularVelocity
                modelbody['linearVelocity'] = body.linearVelocity.tuple()
                if shapelist and len(shapelist) > 0:
                    shapes = []
                    for shape in shapelist:
                        modelshape = {}
                        modelshape['density'] = shape.density
                        modelshape['restitution'] = shape.restitution
                        modelshape['friction'] = shape.friction
                        shapename = shape.__class__.__name__
                        if  shapename == "b2CircleShape":
                            modelshape['type'] = 'circle'
                            modelshape['radius'] = shape.radius
                            modelshape['localPosition'] = shape.localPosition.tuple()
                        if shapename == "b2PolygonShape":
                            modelshape['type'] = 'polygon'
                            modelshape['vertices'] = shape.vertices
                        shapes.append(modelshape)
                    modelbody['shapes'] = shapes

                bodylist.append(modelbody)

        worldmodel['bodylist'] = bodylist

        jointlist = []

        for joint in self.world.GetJointList():
            modeljoint = {}

            if joint.__class__.__name__ == "b2RevoluteJoint":
                modeljoint['type'] = 'revolute'
                modeljoint['anchor'] = joint.GetAnchor1().tuple()
                modeljoint['enableMotor'] = joint.enableMotor
                modeljoint['motorSpeed'] = joint.motorSpeed
                modeljoint['maxMotorTorque'] = joint.maxMotorTorque
            elif joint.__class__.__name__ == "b2DistanceJoint":
                modeljoint['type'] = 'distance'
                modeljoint['anchor1'] = joint.GetAnchor1().tuple()
                modeljoint['anchor2'] = joint.GetAnchor2().tuple()

            modeljoint['body1'] = joint.body1.userData['saveid']
            modeljoint['body2'] = joint.body2.userData['saveid']
            modeljoint['collideConnected'] = joint.collideConnected
            modeljoint['userData'] = joint.userData


            jointlist.append(modeljoint)

        worldmodel['jointlist'] = jointlist

        controllerlist = []
        worldmodel['controllerlist'] = controllerlist

        if serialize:
            addvars = additional_vars
            trackinfo = addvars['trackinfo']
            backup = trackinfo
            for key, info in backup.iteritems():
                if not info[3]:
                    trackinfo[key][0] = info[0].userData['saveid']
                    trackinfo[key][1] = info[1].userData['saveid']
                else:
                    addvars['trackinfo'][key][0] = None
                    addvars['trackinfo'][key][1] = None

            additional_vars['trackinfo'] = trackinfo

        worldmodel['additional_vars'] = additional_vars
        f = open(path,'w')
        f.write(json.dumps(worldmodel))
        f.close()

        for body in self.world.GetBodyList():
            del body.userData['saveid'] #remove temporary data

    def json_load(self, path, serialized=False):
        import json

        self.world.GetGroundBody().userData = {"saveid" : 0}

        f = open(path, 'r')
        worldmodel = json.loads(f.read())
        f.close()
        #clean world
        for joint in self.world.GetJointList():
            self.world.DestroyJoint(joint)
        for body in self.world.GetBodyList():
            if body != self.world.GetGroundBody():
                self.world.DestroyBody(body)

        #load bodys
        for body in worldmodel['bodylist']:
            bodyDef = box2d.b2BodyDef()
            bodyDef.position = body['position']
            bodyDef.userData = body['userData']
            bodyDef.angle = body['angle']
            newBody = self.world.CreateBody(bodyDef)
            #_logger.debug(newBody)
            newBody.angularVelocity = body['angularVelocity']
            newBody.linearVelocity = body['linearVelocity']
            if body.has_key('shapes'):
                for shape in body['shapes']:
                    if shape['type'] == 'polygon':
                        polyDef = box2d.b2PolygonDef()
                        polyDef.setVertices(shape['vertices'])
                        polyDef.density = shape['density']
                        polyDef.restitution = shape['restitution']
                        polyDef.friction = shape['friction']
                        newBody.CreateShape(polyDef)
                    if shape['type'] == 'circle':
                        circleDef = box2d.b2CircleDef()
                        circleDef.radius = shape['radius']
                        circleDef.density = shape['density']
                        circleDef.restitution = shape['restitution']
                        circleDef.friction = shape['friction']
                        circleDef.localPosition  = shape['localPosition']
                        newBody.CreateShape(circleDef)
                newBody.SetMassFromShapes()

        for joint in worldmodel['jointlist']:
            if joint['type'] == 'distance':
                jointDef = box2d.b2DistanceJointDef()
                body1 = self.getBodyWithSaveId(joint['body1'])
                anch1 = joint['anchor1']
                body2 = self.getBodyWithSaveId(joint['body2'])
                anch2 = joint['anchor2']
                jointDef.collideConnected = joint['collideConnected']
                jointDef.Initialize(body1,body2,anch1,anch2)
                jointDef.SetUserData(joint['userData'])
                self.world.CreateJoint(jointDef)
            if joint['type'] == 'revolute':
                jointDef = box2d.b2RevoluteJointDef()
                body1 = self.getBodyWithSaveId(joint['body1'])
                body2 = self.getBodyWithSaveId(joint['body2'])
                anchor = joint['anchor']
                jointDef.Initialize(body1,body2,anchor)
                jointDef.SetUserData(joint['userData'])
                jointDef.enableMotor = joint['enableMotor']
                jointDef.motorSpeed = joint['motorSpeed']
                jointDef.maxMotorTorque = joint['maxMotorTorque']
                self.world.CreateJoint(jointDef)

        self.additional_vars = {}
        addvars = {}
        for (k,v) in worldmodel['additional_vars'].items():
            addvars[k] = v

        if serialized:
            trackinfo = addvars['trackinfo']
            for key, info in trackinfo.iteritems():
                if not info[3]:
                    addvars['trackinfo'][key][0] =  \
                                                self.getBodyWithSaveId(info[0])
                    addvars['trackinfo'][key][1] =  \
                                                self.getBodyWithSaveId(info[1])
                else:
                    addvars['trackinfo'][key][0] = None
                    addvars['trackinfo'][key][1] = None

        self.additional_vars = addvars

        for body in self.world.GetBodyList():
            del body.userData['saveid'] #remove temporary data

    def getBodyWithSaveId(self,saveid):
        for body in self.world.GetBodyList():
            if body.userData['saveid'] == saveid:
                return body