Save Space

Official forum for the Chipmunk2D Physics Library.
Post Reply
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Save Space

Post by eggplantanimation »

Is there an easy way to save/pickle all the objects in a space? THIS IS VERY URGENT :oops:
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Save Space

Post by slembcke »

Sort of. If you save all the bodies (position, rotation, velocity, etc), shapes, and joints you have in the world you will be able to reload a space with them manually. You would have to do this yourself anyway in order to sync up your objects with the Chipmunk ones when you deserialize everything. Chipmunk also has a bunch of internal state like tracking active collisions and such. There is no API to save this, and adding one would be difficult.

So yes, you can save the state of a space, but not perfectly. For most purposes, saving the body, shape and joint placement will be good enough.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Re: Save Space

Post by eggplantanimation »

I think my code is somehow glitched:

Code: Select all

def save_level(self, name):
        try:
            os.remove("tracks/"+name+".marb")
        except:
            pass
        all_list=[]
        for item in self.all:
            if type(item) == pymunk.Segment:
                pv1 = item.body.position + item.a.rotated(body.angle)
                pv2 = item.body.position + item.b.rotated(body.angle)
                p1 = pv1.x, pv1.y
                p2 = pv2.x, pv2.y
                all_list.append(["Line", p1, p2])
                
This is my loader:

Code: Select all

def load_level(self, name):
        store.totald=[0,0]
        for item in space.shapes:
            try:
                space.remove(item, item.body)
            except:
                print
        print "LOADING"
        self.balls=[]
        self.lines=[]
        self.all=[]
        openfile=open("tracks/"+name+".marb","r")
        full_items=pickle.load(openfile)
        for line in full_items:
            body = pymunk.Body(pymunk.inf, pymunk.inf)
            shape= pymunk.Segment(body, line[1], line[2], 0.0)
            shape.friction = 0.99
            space.add(shape)
            store.lines.append(shape)
            store.all.append(shape)
        
Some times the objects come on the screen rotated, and other times they are on the screen (I think), but so far away I can't see them. This only happens after I enable what I call a "Tracking camera":

Code: Select all

if self.tracking:
            if len(self.balls)>0:
                if None in self.old:
                    self.old=[self.balls[-1].body.position.x, self.balls[-1].body.position.y]
                movex=self.old[0]-self.balls[-1].body.position.x
                movey=self.old[1]-self.balls[-1].body.position.y
                for object in self.all:
                    object.body.position.x+=round(movex, 0)
                    object.body.position.y+=round(movey, 0)
                self.totald[0]+=movex
                self.totald[1]+=movey
            else:
                self.tracking=False
That is run every frame. Currently, I have it so it saves the space before you start tracking, and then when you stop, restores from the old file. Is there a better way? This always works. Then, I call the save function again, and give the user the ability to choose the name of the file they are saving to. I close out, and they open it again. When I do, it is either all messed up, rotated at an insane angle, or way off the screen. Help?
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Re: Save Space

Post by eggplantanimation »

Please people, I really need help on this!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Save Space

Post by slembcke »

Did you see this post from just a day or two ago?
http://www.slembcke.net/forums/viewtopic.php?f=1&t=968
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Re: Save Space

Post by eggplantanimation »

That looks like exactly what I want, but it is C++, not python! Do you think it will make it into the next pymunk?
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: Save Space

Post by viblo »

As for your problem, I don't think you need to calculate the endpoint positions before you save it, just save it all as it is like in this code (untested):

Code: Select all

if type(item) == pymunk.Segment:
    pos = item.body.position                 
    angle = item.body.angle
    a = item.a
    b = item.b
    all_list.append(["Line", pos, angle, a, b])
About a save function in pymunk, you are the first one to ask for one :) In my own projects Ive built custom saves just like you... But you are right, an easy way to save/load a space with its contents would have been nice in many cases, at least for smaller programs and test scripts. Usually when I read about some problem or try to implement a new function I need a space with some objects in it anyway, and now I either recreate them by hand each time I open a new interpreter session, or just code it up in a test module and reload. A save/load function would have been nice in that case... Ill add it to my todo, but I won't promise anything. If I hadn't been trying to do some c#-coding for a cert I might have tried to hack something together today, but now it might take a while before I look at it.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Re: Save Space

Post by eggplantanimation »

I got it to work for lines, but when I try it with polygons, it seems that they have the uncontrollable desire to DANCE!!!! They fly around the screen as if they have a low-power motor attached to them, causing them to spin out of control. Please help! My code:

Code: Select all

def create_load_poly(self, points, mass = 5.0, pos = (0,0), moment = None):
        box_points = map(pymunk.Vec2d, points)
        if moment is None:
            moment = pymunk.moment_for_poly(mass, points, pymunk.Vec2d(0,0))
        #moment = 50000
        body = pymunk.Body(mass, moment)
        body.position = pymunk.Vec2d(pos)

        shape = pymunk.Poly(body, box_points, pymunk.Vec2d(0,0))
        shape.friction = 0.5
        shape.collision_type = COLLTYPE_DEFAULT
        shape.kill_me = False
        space.add(body, shape)
        return shape

Code: Select all

    def load_level(self, name):
        for item in space.shapes:
            if item.collision_type != COLLTYPE_MOUSE:
                try:
                    space.remove(item, item.body)
                except:
                    print "Item: "
                    print type(item)
                    print
        for item in self.balls:
            self.balls.remove(item)
        for item in self.lines:
            self.lines.remove(item)
        for item in self.polys:
            self.polys.remove(item)
        self.balls=[]
        self.lines=[]
        self.all=[]
        openfile=open("levels/"+name+".marb","r")
        full_items=pickle.load(openfile)
        print full_items
        for line in full_items:
            if line[0] == "Line":
                print "Line"
                body = pymunk.Body(pymunk.inf, pymunk.inf)
                shape = pymunk.Segment(body, line[1], line[2], 0.0)
                shape.friction = 0.99
                shape.kill_me = False
                space.add(shape)
                store.lines.append(shape)
                store.all.append(shape)
            elif line[0] == "Circle":
                print "Circle"
                p = pymunk.Vec2d(line[1])
                body = pymunk.Body(5, 100)
                body.position = p
                shape = pymunk.Circle(body, 10, (0,0))
                shape.friction = 0.5
                space.add(body, shape)
                store.balls.append(shape)
                store.all.append(shape)
            elif line[0] == "Poly":
                print "Poly"
                print pymunk.util.calc_center(line[2])
                poly = store.create_poly(line[2], 5.0, pymunk.util.calc_center(line[2]), line[3])
                poly.body.reset_forces()
                store.polys.append(poly)
        #print store.all
        print store.polys
Please help, I have been working on this for days with no improvement...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Save Space

Post by slembcke »

I see that you are doing something with calculating the polygon center. It's possible that your center of gravity is off. That could cause the objects to move around somewhat randomly. I would double check that first.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
eggplantanimation
Posts: 12
Joined: Mon Apr 05, 2010 1:59 pm
Contact:

Re: Save Space

Post by eggplantanimation »

Thanks! I figured it out! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests