Create a rotating wheel

Official forum for the Chipmunk2D Physics Library.
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Create a rotating wheel

Post by Thom »

Hello,
I would like to create a rotationg wheel with chipmunk which means one main circle and couple of edges

Code: Select all

// Create the body
	Entity.m_PhysicBody = cpBodyNew(fMass, fMoment);
	Entity.m_PhysicBody->p = [RESOURCES ConvertCocos2DPointToChipmunkPoint:PPosition];
	cpBodySetAngle(Entity.m_PhysicBody, fAngle);
	cpSpaceAddBody(space, Entity.m_PhysicBody);
	
	// Create the Circle
	Entity.m_PhysicShape = cpCircleShapeNew(Entity.m_PhysicBody, Radius, cpvzero);
	Entity.m_PhysicShape->e = 0; 
	Entity.m_PhysicShape->u = 0; 
	
	Entity.m_PhysicShape->data = Entity.m_EntitySprite;
	Entity.m_PhysicShape->collision_type = 0;
	cpSpaceAddShape(space, Entity.m_PhysicShape);
	
	// Create the Rectangles
	// Define our shape's vertexes
	int width = 30;
	int height = 35;
	
	cpVect verts0[4] = {cpv(-width/2,height/2), cpv(width/2, height/2), cpv(width/2, -height/2), cpv(-width/2,-height/2)};
	
	float angle = (2*3.14)/Edges;
	float currentangle = 0;
	int offset = Radius + width/2;
	
	for (int i=0; i<Edges; i++) {
		// Create a obstacle	
		Entity.m_PhysicShape = cpPolyShapeNew(Entity.m_PhysicBody, 4, verts0, cpv(cos(currentangle)*offset,sin(currentangle)*offset));
		Entity.m_PhysicShape->e = 0.3; 
		Entity.m_PhysicShape->u = 0.3; 
		Entity.m_PhysicShape->data = tempsprite;
		Entity.m_PhysicShape->collision_type = 0;
		cpSpaceAddShape(space, Entity.m_PhysicShape);
		
		currentangle += angle;
	}
it's working if the wheel doesn't rotate, as soon as I rotate the body, my ball pass through the edges with a weird behaviour, the ball should bounce on the edge of the wheel.
Thanks for your time
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

oki I've tried a different way but I'm still having trouble.
Just for testing I create my main circle and a polygon. it works fine
And then I try to add a joint between those 2 entities and they both disappear from the screen, theirs bodies positions are NaN

Code: Select all

	cpSpaceAddJoint(space, cpPinJointNew(Entity.m_PhysicBody,tempbody,cpv(Radius,0.0),cpv(-15.0,0.0)));
I'm not sure about the two vectors. It's the offset from the center of the entity right ?
Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Create a rotating wheel

Post by slembcke »

Not quite sure what you are doing exactly. A couple of things I don't understand:

What are the edges for exactly? They aren't square, and you are attaching them around the edge without rotating them. I'm guessing that's not really what you meant. What ball is supposed to bounce? How are you rotating the wheel exactly?

You are also overwriting this instance variable several times in your for loop:

Code: Select all

      Entity.m_PhysicShape = cpPolyShapeNew(Entity.m_PhysicBody, 4, verts0, cpv(cos(currentangle)*offset,sin(currentangle)*offset));
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

The edges are squares attached to the wheel. And I want those squares to rotates with the wheel.
The ball is another physic entity that I've created and which move around. basically you move the ball around on your ipod and the wheel is an obstacle.

To rotate the wheel I do

Code: Select all

m_fangle += 0.1;
cpBodySetAngle(m_PhysicBody, m_fangle);
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Create a rotating wheel

Post by slembcke »

Thom wrote:oki I've tried a different way but I'm still having trouble.
Just for testing I create my main circle and a polygon. it works fine
And then I try to add a joint between those 2 entities and they both disappear from the screen, theirs bodies positions are NaN

Code: Select all

	cpSpaceAddJoint(space, cpPinJointNew(Entity.m_PhysicBody,tempbody,cpv(Radius,0.0),cpv(-15.0,0.0)));
I'm not sure about the two vectors. It's the offset from the center of the entity right ?
Thanks
Yes, the offsets are from the center of each body. What are you doing with tempbody exactly?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

Oh yes sorry I forgot this piece of code

Code: Select all

	float offset = Radius + width/2;

	tempbody = cpBodyNew(fMass, fMoment);
	tempbody->p = cpv(Entity.m_PhysicBody->p.x + (cos(currentangle)*offset) ,Entity.m_PhysicBody->p.y + (sin(currentangle)*offset));
	cpBodySetAngle(tempbody, fAngle);
	cpSpaceAddBody(space, tempbody);
So I add this square to the right of my wheel and I would like to attach it to the wheel, then when I rotate the wheel the square rotates with it.
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

Oki forget about the post before, it's fixed now, I wasn't setting the Masse properly.
But....
When I rotate my wheel without the joint with the function

Code: Select all

cpBodySetAngle(m_PhysicBody, m_fangle);
it works fine.
As soon as I add the PinJoint the rotation of the wheel stop at some point and come back to its initial position, and the square is not rotating with the wheel
Any ideas?
Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Create a rotating wheel

Post by slembcke »

You shouldn't move things by setting position/rotation explicitly. What happens is that you move the object, but you don't update it's rotational velocity. When things collide with it, they won't know how fast it's moving. They will only know that they somehow started overlapping with it and will get pushed apart by a corrective force that is much smaller than the one it needs for a collision with a moving object.

Not quite sure what you are trying to do with the pin joint, but I think maybe you need some other kind of joint. A pin joint attaches the two anchor points on each body by a pin or rod so that the anchor points stay the same distance apart as when the joint was created.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

Ok I attached a sprite of the wheel because I don't think it's clear for everyone.
[img]wheel_large.png[/img]
So I have one main wheel and 4 squares around, I want when I rotate the wheel taht the squares rotates as well.
What kind of joint do you recommend ?
Thanks a lot for your help
Thom
Posts: 11
Joined: Tue Nov 03, 2009 6:08 am
Contact:

Re: Create a rotating wheel

Post by Thom »

Alright I fixed it !!!
I create my squares around my circle and they all have the same PivotJoin it's works perfectly now.
Thank you for your help
Bye
Post Reply

Who is online

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