modeling a pinball flipper (pymunk) / was Re: trouble with [

Official forum for the Chipmunk2D Physics Library.
beeduul
Posts: 5
Joined: Sun Jan 17, 2010 12:41 pm
Contact:

modeling a pinball flipper (pymunk) / was Re: trouble with [

Post by beeduul »

[edit: renamed thread -- formerly: trouble with PinJoint and SlideJoint (pymunk)]

i'm using pymunk to get at my chipmunk physics, but the concepts should be the same. i'm using pymunk 0.8.4 which is using chipmunk 4.1.0.

i'm modeling a flipper-like object, basing it off the demo_slide_and_pinjoint.py example that comes with pymunk. basically, i have a flipper body (with a single segment for a shape). that flipper body is PinJointed to an infinitely massive/momented body, both anchors are at 0, 0 on the two bodies. this makes the flipper pivot around the pin. all is good so far.

next, i add a SlideJoint, creating another infinitely massive/momented body some distance out from the flipper's center to act as the constraint. i'm using 0, 0 as the anchor points on both the flipper and the constraint body, and set the slide min/max to 0 and some reasonable distance. but when i run my test again with the SlideJoint the flipper body is wildly unstable, jittering all around it's expected PinJointed position.

i have poured over the pymunk demo_slide_and_pinjoint.py example to try to figure out why that code works and mine doesn't. something that i noticed is that in the example, the anchor point on the L-shape body in the SlideJoint is offset (-100, 0). if i change that -100 to anything greater than about -70, it becomes wildly unstable, too. try changing the line in the example code so that the anchor is (-65,0) and re-run it:

rot_limit = pm.SlideJoint(body, rot_limit_body, (-65,0), (0,0), 0, joint_limit)

see, it's bouncing all over the place!

i would love any and all ideas/conjectures/solutions/explanations.
Last edited by beeduul on Tue May 18, 2010 5:59 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by slembcke »

Is this like a fast moving pinball flipper? If so, this thread may be useful to you: http://www.slembcke.net/forums/viewtopic.php?f=1&t=787
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by viblo »

As I couldn't resist (have to get up early tomorrow for work and we all know its most fun to code useless scripts like this one way past bedtime :D) I created a little demo using the trunk version of pymunk here: http://pastebin.com/m666d88d4
I used a a paddle in the form of a triangle shaped polygon and attached it with a PinJoint together with a DampedRotarySpring to a static body. Then I apply an impulse to generate some rotation when the user activate it. And as a little bonus two bumpers (static circle with elasticity = 1.5).

slembcke: I had to cheat a little and reset the paddle position/velocity each frame to prevent the paddles to wobble. Its like the pinjoint doesnt manage to align the paddle back properly the last 0.0001 units or something?

With some kind of cooldown to prevent repeated shoots and some way for user to keep the paddle uppright I think it would work pretty good. Maybe the manual velocity adjustment works even better, but I thought it was more interesting to simulate the paddles with springs/impulses. :)

(I am very soon to release the new version of pymunk, I think I have nailed everything except for a small win 64bit thing, so it is ready to be released as soon as I got a little time over to actual do it. If you want a sneak preview you can get the latest source from its project place, see http://code.google.com/p/pymunk/source/checkout)
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
beeduul
Posts: 5
Joined: Sun Jan 17, 2010 12:41 pm
Contact:

modeling a pinball flipper / was Re: trouble with [...]

Post by beeduul »

Hi guys, thanks for following up. I got sidelined and am just getting back to this project.

slembcke, I created a manually controlled flipper like you suggested in the other thread @ viewtopic.php?f=1&t=787. The control is great, but I get a lot of 'stickiness' -- the ball tends to grip to the flipper instead of being hoisted up. Note, friction is set to 0 and elasticity to .95 for both the flipper and the ball (I figured I'd start conservative and tune down.) Do I have to impart forces manually upon collision in order to 'whack' the ball with the flipper? Sorry, I am still fumbling my way through chipmunk & game physics simulations in general.

viblo, your pastebin link is no longer working. I'd love to see your code. Do you still have it around?

thanksthanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by slembcke »

are you setting the angular velocity (body.w) of the flipper's body? If you aren't, then Chipmunk will find a collision, but not between moving objects. The only way that Chipmunk knows to pass any movement on from one object to another is if it has a velocity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
beeduul
Posts: 5
Joined: Sun Jan 17, 2010 12:41 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by beeduul »

yep, i am. let me clarify though -- the angular velocity should just be the delta between the last and current frame's flipper angle, right?

additionally i'm frame rate locking to 60 hz and sub-stepping the chipmunk space 20 (i've tried 1, 10, and 100 as well) times each frame.

sometimes the fast moving flipper misses collision with the ball (more so if i shrink its radius). and other times it 'grabs', like i mentioned before. then instead of being launched upwards once the activated flipper reaches its max position, the ball lurches to the side.

i could post the code if you want to try running it to see what i mean. it's python/pymunk using pyglet for graphics though so you'd have to have those installed.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by slembcke »

It's the delta angle divided by the timestep (the dt parameter to cpSpaceStep).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
beeduul
Posts: 5
Joined: Sun Jan 17, 2010 12:41 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by beeduul »

right -- i'm calculating the angle delta at each frame, using dt. i have tried a couple of different techniques - stepping the flipper once per frame, or sub-stepping it once for each sub-step of cpSpaceStep (or Space.step, in pymunk parlance).

if you feel so inclined, you can check out the code at: http://pastebin.com/Fr74NLmX
Required: pyglet & pymunk (and python, of course!)
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by slembcke »

I think you still misunderstood. You are doing this in your code:

Code: Select all

 self._body.angular_velocity = newAngle - self._body.angle
You need to do this:

Code: Select all

 self._body.angular_velocity = (newAngle - self._body.angle)/dt
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
beeduul
Posts: 5
Joined: Sun Jan 17, 2010 12:41 pm
Contact:

Re: trouble with PinJoint and SlideJoint (pymunk)

Post by beeduul »

wow, that's a thousand times better!

angular velocity is expressed in seconds, so you have to divide by dt to express it in seconds, instead of 1/60th (or whatever timestep) of a second. is that right? i was not grasping that before.

alright, now to get whacking.

thanks slembcke!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests