Moving shapes with a mouse.

Official forum for the Chipmunk2D Physics Library.
Post Reply
kablooie
Posts: 1
Joined: Wed Jun 24, 2009 10:39 am
Contact:

Moving shapes with a mouse.

Post by kablooie »

I'm trying to move shapes with a mouse and use chipmunk to handle collisions like an air hockey game.

When I move a shape it jitters and jumps around.
I'm using cpBodySlew each 60th of a second for the movement.

I can't find away to detect collisions and have smooth control of the shape's position.

Anyone have suggestions as to how to handle this?
cheery
Posts: 2
Joined: Wed Jun 24, 2009 4:36 pm
Contact:

Re: Moving shapes with a mouse.

Post by cheery »

Code: Select all

class GodFinger(object):
    def __init__(self):
        self.body = pymunk.Body(pymunk.inf, pymunk.inf)
        self.body.set_velocity_func(self.update_velocity)
        self.joint = None
        self.point = Vec2d(0.0, 0.0)

    def update_velocity(self, body, gravity, damping, dt):
        difference = self.point - body.position
        old_velocity = body.velocity
        new_velocity = difference * (1.0 / dt) * 0.3
        body.velocity = old_velocity * 0.7 + new_velocity * 0.3
        self.moved = Vec2d(0.0, 0.0)

    def grab(self, shape, point):
        if not self.joint:
            self.joint = pymunk.PivotJoint(self.body, shape.body, point);
            space.add(self.joint)

    def on_mouse_release(self, x, y, button, modifiers):
        if self.joint:
            space.remove(self.joint)
            self.joint = None

    def on_mouse_motion(self, x, y, dx, dy, *_):
        self.point = Vec2d(x, y)

    def on_mouse_press(self, x, y, button, modifiers):
        space.nonstatic_point_query((x,y), self.grab, Vec2d(x,y))

    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        self.point = Vec2d(x, y)
I've used this kind of thing. It's a direct translation from an old cpMouse algorithm I copied from these forums.

In the svn repository there seem to be cool toys that are more advanced than the current things I have.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests