Page 1 of 2

Making a paddle...

Posted: Tue Nov 17, 2009 8:57 am
by mani2009
Hi, just started using chipmunk( good work btw), and i am try to make a paddle for a pinball type game.
Have started with a rectangle using the demo code but i am a bit lost on how to make a pivot type motion.
Demo 7 has a pivot joint example but it looks more like a rope than a paddle.
Demo 8 has apply force example to make the paddle move "body->velocity_func = apply_force;"
It is basically a "door opening" type movement i want to create. Any ideas?

this is what i have so far

Code: Select all

	int num = 4;
	cpVect verts[] = {
		cpv(-15,-15),
		cpv(-15, 15),
		cpv( 35, 15),
		cpv( 35,-15),
	};

	body = cpBodyNew(1.0, cpMomentForPoly(1.0, num, verts, cpvzero));
	body->p = cpv(-280, 240);
                body->velocity_func = apply_force;
	cpSpaceAddBody(space, body);
	shape = cpPolyShapeNew(body, num, verts, cpvzero);
	shape->e = 0.0; shape->u = 1.5;
	shape->collision_type = 1;
	cpSpaceAddShape(space, shape);

Re: Making a paddle...

Posted: Tue Nov 17, 2009 10:21 am
by slembcke
Here is how I would do it. Create a body with an infinite mass and moment of inertia, this is the body for the flipper. Don't add the body to the space because you are going to control it manually instead. Create the shape for the paddle, a simple fat line segment or box will do. Add the shape as a normal shape as it will be moving around. Make the center of gravity to be where you want it to pivot. That way all we have to do is rotate the body, and the shape will rotate around the pivot point.

Now, to control the body, you will have to update both it's rotation and angular velocity. When a key is pressed, rotate the body towards the activated position a little each frame. Calculate the angular velocity as (newAngle - oldAngle)/timestep. When the key is released, rotate it back the other way.

Re: Making a paddle...

Posted: Tue Nov 17, 2009 1:21 pm
by mani2009
Thanks, got it working, any idea what values of e and u to use to model a pinball? or is it to do with the gravity value set for the space?

Re: Making a paddle...

Posted: Tue Nov 17, 2009 2:23 pm
by slembcke
Not really sure. I'd probably start with e=0.6 and u=0.4. That is just an educated guess though. Experiment and adjust.

Re: Making a paddle...

Posted: Wed Nov 18, 2009 6:08 am
by mani2009
Thanks

Re: Making a paddle...

Posted: Mon Dec 14, 2009 7:11 am
by pabloruiz55
Sorry to hijack this thread but i also need to make a flipper for a pinball game and was playing with that.

Is it possible to create a pin joint at the point it should rotate and then on update check if the rotation is higher or lower than some number and don't let it rotate past that number?
Is there another way of not letting it rotate past certain angles?
If i do this i would only have to apply an impulse to the other side of it to make it hit the ball.


If i go the "static shape with manual movement" way, would the hitting part be correct? i mean if the ball is near and i make it hit it by adjusting the rotation manually would it hit the ball correctly? I it doesn't, how could i apply an impulse to the ball that simulates the correct behaviour?

Many thanks!

Re: Making a paddle...

Posted: Mon Dec 14, 2009 9:37 am
by slembcke
In Chipmunk 5 there are angular limit joints that will let you limit the rotation.

For something fast moving like the pinball paddle, I'd still go with the manually controlled approach. It's a little more work, but a lot more likely to look realistic instead of really bouncy/spongy.

Re: Making a paddle...

Posted: Tue Dec 15, 2009 5:10 pm
by pabloruiz55
Hi, thanks for the answer!

I am trying to do what you posted before, but how can i change the center of gravity so when rotating it doesn't rotate form the center but from the "anchor" ?

Re: Making a paddle...

Posted: Tue Dec 15, 2009 5:33 pm
by pabloruiz55
Ok, i figured that out.

The problem is that i did everything that is posted here but manually rotating the flipper is making the ball go through it... any idea? Thanks!

Re: Making a paddle...

Posted: Wed Dec 16, 2009 9:48 am
by slembcke
You mentioned "static shape" before. Are you actually adding the paddle as a static shape? Static shapes don't update their collision detection info and cannot be moved. You need to add them as normal shapes.