Am I doing rouge shape correctly?

Official forum for the Chipmunk2D Physics Library.
Post Reply
Doodlemeat
Posts: 2
Joined: Sun Mar 16, 2014 3:50 am
Contact:

Am I doing rouge shape correctly?

Post by Doodlemeat »

Hello.
I am very new to Chipmunk and I wonder how you create rouge shapes. From what I could read, rouge shapes are shapes that its body are not added to the space. But you still create their bodies?

Let me explain my need.
Right now, I have made so that I can spawn small balls into a rectangle and fill it up.
But now I want to create a polygon cog that will spin around inside the rectangle and tumble around the balls. You understand? I think I have achieved it, but now fully. Right now I just have a polygon in form of a rectangle and whenever I rotate it, the balls goes trough the rectangle like jelly almost. Let me show you the code.

This is me, creating a rouge shape. I never add it to space.

Code: Select all

cpVect* vertices = new cpVect[4];
	vertices[0].x = 0; vertices[0].y = 100;
	vertices[1].x = 100; vertices[1].y = 100;
	vertices[2].x = 100; vertices[2].y = 0;
	vertices[3].x = 0; vertices[3].y = 0;
	cpBody* cBody = cpBodyNewStatic();
	cpBodySetPos(cBody, cpv(300, 300));

	cpShape* cShape = cpPolyShapeNew(cBody, 4, vertices, cpvzero);
	cpSpaceAddShape(space, cShape);
	cpShapeSetElasticity(cShape, 0);
	cpShapeSetFriction(cShape, 0);
This is me, rotation the rouge shape.

Code: Select all

		cpBodySetAngle(cBody, thor::toRadian<float>(45));
		cpSpaceReindexShapesForBody(space, cBody);
So to my final question, is this really the correct way of doing this?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Am I doing rouge shape correctly?

Post by slembcke »

Since the body is changing, you don't want to make a static body (static means something that doesn't change). If you create a normal body, you won't need to call cpSpaceReindexShapesForBody() each time you move the body.

Secondly, if you change the position/angle of a rogue body you usually also want to set the velocity/angular velocity to match. Otherwise the body will act like it teleports from place to place. When you teleport two objects into the same place, they mush apart like jelly as you say.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Doodlemeat
Posts: 2
Joined: Sun Mar 16, 2014 3:50 am
Contact:

Re: Am I doing rouge shape correctly?

Post by Doodlemeat »

But say you want to make a platformer with a platform that moves back and forth. That body should not be affected with gravity or any other body that touches it. So how are you creating that without making it static? :O
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Am I doing rouge shape correctly?

Post by slembcke »

That's what rogue bodies are for. Because you don't add them to the space, they won't be simulated normally and you can update it as you want. Shapes attached to rogue bodies still react normally however.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests