Initial Fixture Positions for Polygons wrong..?

Discuss issues specific the Python port of Box2D
Henry Jones Jr.
Posts: 1
Joined: Tue Feb 26, 2013 4:00 am

Initial Fixture Positions for Polygons wrong..?

Postby Henry Jones Jr. » Tue Feb 26, 2013 4:17 am

Hi there,

I am currently having a look @ pyBox2D and I like it, cool thing. So I made a little program using Pygame. When you click the Mouse, a Body is created randomly, there are 3 different types of Bodies: Boxes, Balls, and Polygons. I always create the Body on the mouse cursor position. Now the weird thing (which is really starting to annoy me cause I don't know what's wrong with my code??) is that Balls and Boxes work fine; they are created @ the mouse cursor's position and behave according to gravity, fine. But my Polygons - even though they are created @ the very same initial position, seem not to be generated at all at first; but after a while they fall down from above. So they are created much too high (high real world y)... the x coordinate is not a 100% fine either, the body is created with the left side on the mouse cursors x pos - it should be centered though.

I am sure that there must be some logical reason why this behaves kinda wrong only when I created Polygons, but I really do not find out... I sit perhaps because of CCW (counter clockwise winding) ? I have even tried it the other way round, but still the initial position is wrong. As Fixtures created are atached to bodies in relation to their pos of origin and the body's position itself is correct, there MUST be something wrong with the center of that polygon shape I guess..?

Here's the code in the program which creates the bodies:

Code: Select all

    def make_ball(self):

        r = random.randint(10, 35)
        self.b2objects.append(Dynamic_Ball(r, self.mouse.pos, rest=0.8))

    def make_box(self):

        w_min = h_min = 10
        w_max = h_max = 50
        w = random.randint(w_min, w_max)
        h = random.randint(h_min, h_max)
        self.b2objects.append(Dynamic_Box(w, h, self.mouse.pos))

    def make_polygon(self):

        poslist = ((0, 0), (0, 20), (73, 20), (73, 13), (66, 8), (36, 0))
        self.b2objects.append(Dynamic_Polygon(poslist, self.mouse.pos))


and here are the classes for Bodycreation I wrote:

Code: Select all

class Dynamic_Box():                                   

    def __init__(self, width, height, centerpos, colour=None,
                 dens=1, frict=0.5, rest=0.25):

        b2_centerpos = get_b2pos(centerpos)
        b2w, b2h = width / PPM / 2.0, height / PPM / 2.0
       
        self.body = world.CreateDynamicBody(position=b2_centerpos)
        self.body.CreatePolygonFixture(box=(b2w, b2h), density=dens, friction=frict, restitution=rest)
        if not colour:
            self.colour = colour_x(gamma_min=150)
        else:
            self.colour = colour

class Dynamic_Ball():

    def __init__(self, radius, centerpos, colour=None,
                 dens=1, frict=0.5, rest=0.25):

        b2_centerpos = get_b2pos(centerpos)
       
        r = radius / PPM
        self.body = world.CreateDynamicBody(position=b2_centerpos)
       
        circle = b2CircleShape(pos=(0, 0), radius=r)
        ball = self.body.CreateFixture(shape=circle, density=dens, friction=frict, restitution=rest)
        if not colour:
            self.colour = colour_x(gamma_min=150)
        else:
            self.colour = colour

class Dynamic_Polygon():

    def __init__(self, poslist, centerpos, colour=None,
                 dens=1, frict=0.5, rest=0.25):

        b2_centerpos = get_b2pos(centerpos)
       
        self.body = world.CreateDynamicBody(position=b2_centerpos)
        vert = []
        for pos in poslist:
            vert.append(get_b2pos(pos))
        polygon_shape = b2PolygonShape(pos=(0, 0), vertices=vert)
        self.body.CreateFixture(polygon=(vert), density=dens, friction=frict, restitution=rest)
        if not colour:
            self.colour = colour_x(gamma_min=150)
        else:
            self.colour = colour


I would be very, very thankful if someone could have a look ath this and tell me why only the Polygons seem to have a wrong initial position.

Thank you, regards,


Henry

Romance1984
Posts: 5
Joined: Sat Jul 25, 2015 2:42 am

Re: Initial Fixture Positions for Polygons wrong..?

Postby Romance1984 » Tue Sep 06, 2016 12:24 am

the Polygons seem to have a wrong initial position.
วิธีเล่นบาคาร่าให้ได้เงิน


Return to “Python”



Who is online

Users browsing this forum: No registered users and 1 guest