Simple rotating wheel

Official forum for the Chipmunk2D Physics Library.
kettch
Posts: 5
Joined: Tue Aug 11, 2009 6:53 am
Contact:

Simple rotating wheel

Post by kettch »

Hi,

I'm trying to create a simple rotating wheel, but I always end up having my wheel going everywhere, although I'd like it not to move, but just rotate.
What I tried so far is creating a static circular shape at (0,0), with a body of infinite mass and inertia, and my wheel, circular too (obviously), also centered on (0,0), and both are linked together with a pivot joint at anchors (0,0) and (0,0).

When I apply an impulse on the wheel, it rotates but it also moves around... Is my approach wrong, is there some better way to achieve that ? Or did I just mess up with the way I implemented it ?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simple rotating wheel

Post by slembcke »

Can you post some code?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
maximile
Posts: 157
Joined: Mon Aug 20, 2007 12:53 pm
Location: London, UK
Contact:

Re: Simple rotating wheel

Post by maximile »

Here's the code I use to do it, if you only want it moving at a constant speed.

Code: Select all

// addObject method, called once:
{
	body = cpBodyNew(INFINITY,INFINITY); // don't add the body to the space
	body->w = self.speed;
	body->p = cpv(xPos,yPos);

	cpShape * shape = cpCircleShapeNew(body,radius,cpvzero);
	cpSpaceAddShape(space, shape);
}

// physicsStepped method, called after cpSpaceStep returns
{
	cpBodyUpdatePosition(body, dt);
}
Works great for me, but if you actually want it to respond to forces and collisions and so on I guess it won't.
kettch
Posts: 5
Joined: Tue Aug 11, 2009 6:53 am
Contact:

Re: Simple rotating wheel

Post by kettch »

That's the point: I want it to respond to forces applied to it.
Here's the code I'm using so far :

Code: Select all

// the wheel, centered on (0,0), radius 400
cpBody *wheelBody = cpBodyNew(0.3, cpMomentForCicle(0.3, 0.0, 400.0, cpv(0,0)));
cpShape* wheelShape = cpCircleShapeNew(wheelBody, 400.0, cpv(0.0, 0.0));

// the static body that should hold it in place, centered on (0,0), radius 1
cpBody *staticBody = cpBodyNew(INFINITY, INFINITY);
cpShape *staticShape = cpCircleShapeNew(staticBody, 1.0, cpv(0.0, 0.0));

// the joint between the two
cpConstraint *joint = cpPivotJointNew2(wheelBody, staticBody, cpv(0.0, 0.0), cpv(0.0, 0.0));

// then I add all of them
cpSpaceAddBody(mySpace, wheelBody);
cpSpaceAddShape(mySpace, wheelShape);
cpSpaceAddBody(mySpace, staticBody);
cpSpaceAddStaticShape(mySpace, staticShape);
cpSpaceAddConstraint(mySpace, joint);

// then I apply an impulse on the wheel
cpBodyApplyForce(wheelBody, cpv(0.0, 20.0), cpv(150.0, 0.0));
The last line is just a fast test. I mainly use the same code that is used in chipmunk demos to pick an object and move it around.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simple rotating wheel

Post by slembcke »

wheelShape and staticShape are colliding. It's possible to have a body without a shape attached, this is useful thing when working with joints.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kettch
Posts: 5
Joined: Tue Aug 11, 2009 6:53 am
Contact:

Re: Simple rotating wheel

Post by kettch »

Thanks slembcke, it's working better now. I'm still seeing small position changed on my wheel, but I'm just ignoring it for the drawing. Hopes this has no other impact.

Still, the wheel sometimes behaves weirdly, especially when I'm using clicks/touches while it's rotating. It sometimes goes the other way, or things like that. The code there is the same as in ChipmunkDemo.c from trunk release 204.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simple rotating wheel

Post by slembcke »

Joints don't hold objects together exactly, they just keep them together as well as possible and fix the error over time.

Without knowing what exactly it is that you are doing when applying touches I have no idea why it would switch direction etc.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kettch
Posts: 5
Joined: Tue Aug 11, 2009 6:53 am
Contact:

Re: Simple rotating wheel

Post by kettch »

I am doing exactly what you are doing in the demo code, but with touches :
- when the touch starts, I create a body of infinite mass, infinite inertia, set its position at the touch. Then I pick the shape that was touched (either my wheel or nothing), create a pivot joint linking the two (the touch body at (0,0) and the wheel at the position of the touch)
- when the touch moves, I smooth the move as you do in your code, update the touch body position and velocity
- when the touch ends, I just destroy the touch body

I can send the code if you wish, but on that point, it comes straight from the chipmunk demo code.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Simple rotating wheel

Post by slembcke »

Are you using force limits? Without them, two joints with unbounded force limits are going to have a nice fierce fight.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
kettch
Posts: 5
Joined: Tue Aug 11, 2009 6:53 am
Contact:

Re: Simple rotating wheel

Post by kettch »

Actually, I changed the code to construct my wheel a bit, since you said that a joint is meant to keep objects together as well as possible. Thus, I removed the static body and the joint that linked it to the wheel, and I also changed the wheel's position_func to one that does exactly the same as the standard, but without updating the position. So now there's only the touch joint involved.

And no, I'm not using force limits.

As a matter of fact, I'm starting to believe that using chipmunk for just rotating a wheel might be a little too much, and I might as well extract the important formulas and simplify them in my special case...
Post Reply

Who is online

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