Creating Movable Platform

Official forum for the Chipmunk2D Physics Library.
Post Reply
sdnofootbird
Posts: 2
Joined: Tue Apr 12, 2011 5:39 am
Contact:

Creating Movable Platform

Post by sdnofootbird »

Hi all,
I want to build a movable platform which will move slowly in the air horizontally. There is no rope to hang this platform, but I was wondering how can I eliminate the effect of gravity, or the platform will drop to the ground. Using another cp_space is not feasible, cause I want to detect the collision between platform and other bodies. The mass of such platform can be infinite, is there any suggestions about creating such platform?
Thank you in advance! I really appreciate every suggestion.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Creating Movable Platform

Post by slembcke »

First of all, you should read the "Rogue and Static Bodies" section in the documentation here: http://files.slembcke.net/chipmunk/rele ... s/#cpSpace

Basically you will create an infinite mass rogue body to handle the platform. It's infinite mass because you don't want collisions with other objects to affect it. You don't add it to the space because you don't want it to be simulated and fall under gravity. Instead, you will be simulating it by hand by setting it's velocity and position every frame. Your game's update loop might look something like this:

Code: Select all

void Update(){
  // update the platform
  platformBody->v = cpvmult(cpvsub(newPosition, platformBody->p), 1.0f/dt);
  platformBody->p = newPosition;
  
  // simulate the rest of the space
  cpSpaceStep(space, dt);
}
You do have to be careful about one thing. You must make sure that you don't cause any collisions between infinite mass (or static) objects. What happens when an immovable object meets an unstoppable force? Well, it's unsolvable and you end up with NaNs (very bad). If they do need to overlap, you can use collision groups, layers or callbacks to filter their collisions out. The easiest way is to put all of your infinite mass and static objects into a collision group or simply.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
sdnofootbird
Posts: 2
Joined: Tue Apr 12, 2011 5:39 am
Contact:

Re: Creating Movable Platform

Post by sdnofootbird »

Thank you for your help.

I have created such a platform and found a strange thing. If I set the mass of such platform small like 10, it will easily go through the chain made by me, which is composed by several rigid bodies with constraints. While if I set the mass very large like 10000 or more, it will lift up the chain when passing it, so I was wondering what to expect when a rigid body not in the physical space collides with a rigid body in the physical space.

Thank you for your kind help in advance! I really appreciate your help.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Creating Movable Platform

Post by slembcke »

When it's not added to the space, it won't be have it's velocity or position updated by the space. It will still have collisions applied with it though. If it's supposed to be an immovable object, but has a very low mass then things will get screwed up. That's why you want to make objects controlled like that infinite mass.
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 4 guests