I honestly don't know where to start...

Official forum for the Chipmunk2D Physics Library.
GlynRichards
Posts: 12
Joined: Wed Oct 20, 2010 8:25 am
Contact:

I honestly don't know where to start...

Post by GlynRichards »

I have a simple idea, but as a complete newcomer to all things physics I simply don't know where to start. Essentially I want objects to climb up the screen (under programmatic control) and I want objects to fall from the top and when the falling objects hit the climbing objects I want them to start falling and if the falling objects hit other climbing objects then I want them to all start falling if you see what I mean.

I'm a little confused about the climbing objects and how to stop them from initially falling until they're hit. The falling things I can do. The collisions I can do (I think). But the climbing things have me stumped. Do I need to create them as static objects first and then convert them when a collision ofccurs, but my understanding is that if they're static I won't be able to do the collision detection.

So in essence how do I convert manually climbing objects to physics driven falling objects when hit by another falling object? I'm using a cocos2d/chipmunk playform.

PS: I'm happy to pay a small donation for a good solution.
bollu
Posts: 37
Joined: Tue Sep 14, 2010 4:33 am
Contact:

Re: I honestly don't know where to start...

Post by bollu »

Hmm... sounds like a weird problem :D (no offence). I think you can make the objects climb by creating them normally with Shapes and Bodies and then applying a Force in the direction you want them to move.

When the two objects collide, you can use a Collision Handler to detect the collision, nullify the upward force with cpBodyResetForces(); and then apply a downward force.

Hope this helps!
~Bollu
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I honestly don't know where to start...

Post by slembcke »

Bollu's suggestion is probably the easiest way to do it. You just have to figure out how to calculate the forces correctly.

A better way would be to control your climbers using constraints like in the tank demo. This will give you nice smooth movement without having to calculate forces yourself, but is fairly advanced Chipmunk usage.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
viblo
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm
Contact:

Re: I honestly don't know where to start...

Post by viblo »

It might be a good idea to not add the bodies of the climbers to the space until they are hit with the falling objects.. That way they won't be affected by gravity and it will be a bit easier to reason about the force needed to have them move up.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
GlynRichards
Posts: 12
Joined: Wed Oct 20, 2010 8:25 am
Contact:

Re: I honestly don't know where to start...

Post by GlynRichards »

viblo wrote:It might be a good idea to not add the bodies of the climbers to the space until they are hit with the falling objects.. That way they won't be affected by gravity and it will be a bit easier to reason about the force needed to have them move up.
I didn't realise that I didn't have to add the bodies to the space. I've modified the Simple demo so that a climber would respond to the arrowDirection in the update function and so far this nearly gives me what I need. Although whether this is right I don't know. I'm guessing that I can use playerUpdateVelocity / velocity_func later on for this.

If I don't to the

if (!cpveql(arrowDirection, cpvzero))
ballBody->v = cpvmult(arrowDirection, 150);

then the climber starts to fall so I could just turn this off when a collision occurs. The other problem is that a climber would need to affect climbers below it would this happen if they had the same collision_type? Or would I have to then roll-my-own collision code?

Thanks. The suggestions have been great so far. I'm aware that at the moment I don't know what is right or wrong and how you should do something as opposed to how you shouldn't if you see what I mean!
GlynRichards
Posts: 12
Joined: Wed Oct 20, 2010 8:25 am
Contact:

Re: I honestly don't know where to start...

Post by GlynRichards »

viblo wrote:It might be a good idea to not add the bodies of the climbers to the space until they are hit with the falling objects.. That way they won't be affected by gravity and it will be a bit easier to reason about the force needed to have them move up.
Actually, being a bit thick here!!! How do I does this? Surely if its not added to the space I cannot detect collisions?
GlynRichards
Posts: 12
Joined: Wed Oct 20, 2010 8:25 am
Contact:

Re: I honestly don't know where to start...

Post by GlynRichards »

Right, think I've got it! Dont use cpSpaceAddBody to add the body to the space, but still use cpSpaceAddShape. This will allow collisions, yes? Manually position the climbers my self using the position of the body ->p up until the point that they collide then add the body to the space and stop manually updating the position?

Whaddya reckon?
bollu
Posts: 37
Joined: Tue Sep 14, 2010 4:33 am
Contact:

Re: I honestly don't know where to start...

Post by bollu »

Where are all these demos anyway? They're all in one huge file, right? It's hard to figure anything out through them IMHO :)
GlynRichards
Posts: 12
Joined: Wed Oct 20, 2010 8:25 am
Contact:

Re: I honestly don't know where to start...

Post by GlynRichards »

bollu wrote:Where are all these demos anyway? They're all in one huge file, right? It's hard to figure anything out through them IMHO :)
Nearly there, although it feels like I'm wading through treacle (mollasses for some of you) at the moment. Oh! And that work stuff keeps getting in the way of this which is FAR more interesting.!!!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: I honestly don't know where to start...

Post by slembcke »

I guess that should have been an obvious possibility too.

In Chipmunk, you can attach a collision shape to a body even if the body is not added to the space. Such a body is called a rogue body. The body isn't simulated, meaning it doesn't fall under gravity, react to forces, and it's position and velocity don't get updated. This is useful because you can move the bodies around how you want without having to worry about the physics too much. Rogue bodies are usually given an infinite mass and moment of inertia when you create them so that other objects treat them as solid immovable objects when they hit the rogue body.

There are a couple of things to keep in mind with rogue bodies. While you can set the position directly if you want, you should also be setting the velocity to match the change in position so collision happen correctly. Collisions with rogue bodies will still change a rogue body's velocity if it has a finite 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 3 guests