Newbie elementary stuff

Official forum for the Chipmunk2D Physics Library.
Post Reply
abhikabhie
Posts: 2
Joined: Fri Feb 25, 2011 3:08 am
Contact:

Newbie elementary stuff

Post by abhikabhie »

People, I'm new to chipmunk and objective c and trying to write
A simple game. I have a few balls moving on the screen and have
Added a platform as a rogue body. I.e. It's a initstatic and has not
Been added to the space. Have 3 polyshapes attached to it. I want
The user to be able to move the platform using a pangesture. My
Ques is how do I get the shapes/body to keep up with the movement
Of the associated image? If there are more than one ways to do this,
Which is recommended?

Btw, I'm using objective cm for iPhone.

UPDATE: Still appreciate an answer to the above. In the meanwhile, instead of the initstatic, i'm now using a body (not added to the space) with a very high mass for my moving rogue body. Also, updating the body.pos to match the uipangesture translation. This seems to work however when i pan the rogue body (a bowl) too fast, any balls within it fall out (through the shapes)..... This is probably because, the pangesture method is called too frequently while the space is updated slower..i tried calling the transform method (for all the balls in the space) within the pangesture but this does not seem to help. How do i keep the pangesture moving roque in sync with the space updation (assuming that is the problem) ?
Last edited by abhikabhie on Sat Mar 05, 2011 7:30 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Newbie elementary stuff

Post by slembcke »

Hey, I'm at GDC this week and didn't bring my main development machine with so it's harder to answer more technical questions. I made myself a reminder, but give the thread a nudge if you haven't gotten an answer by Monday.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
abhikabhie
Posts: 2
Joined: Fri Feb 25, 2011 3:08 am
Contact:

Re: Newbie elementary stuff

Post by abhikabhie »

Is this the nudge you are looking for Scott? :roll:
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Newbie elementary stuff

Post by slembcke »

I spent almost the entire day yesterday answering emails and such. I knew I forgot something. :-\

Ok so it seems that you figured out part of the problem. If you use create a static body, you are telling Chipmunk that the body (and the shapes attached to it) are static and not-moving. If you do move the body, the shapes won't be updated unless you do it manually.

For something like a moving platform, you want to create an infinite mass rogue body like this [[ChipmunkBody alloc] initWithMass:INFINITY andMoment:INFINITE]. (More information on what that means here: http://files.slembcke.net/chipmunk/rele ... s/#cpSpace) By giving it an infinite mass and moment of inertia it makes the body so that it doesn't rotate or move in response to collisions.

The second problem you are having now is that you need to update the velocity along with the position. By changing only the position you are causing the shapes to teleport instead of move smoothly. Chipmunk doesn't know if you meant it to move immediately to the new position or if you want the velocity to match the change. So you must explicitly set the velocity to match the position change. Another problem is that input events are generally very very coarse. The pan gesture probably only sends you events at a fraction of the 60Hz rate the screen is running at. This is true of mouse input on computers and touch events on iOS. I assume that the pan gesture probably operates similarly. This means that you need to smooth out the input values. The Chipmunk Demo app does both of these things for the mouse body. ChipmunkDemo.m in the iPhoneChipmunk project (Should be bundled with the Objective-Chipmunk distribution) does it like this:

Code: Select all

- (void)update
{
	cpVect newPoint = cpvlerp(mousePoint_last, mousePoint, 0.25); // 1.0 means no smoothing, 0.0 would be infinite smoothing
	mouseBody.pos = newPoint;
	mouseBody.vel = cpvmult(cpvsub(newPoint, mousePoint_last), 1.0/timeStep);
	mousePoint_last = newPoint;
	
	[space step:timeStep];
}
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 6 guests