Right triangle fall through other shapes

Official forum for the Chipmunk2D Physics Library.
Post Reply
cyiss
Posts: 2
Joined: Tue May 31, 2011 4:33 am
Contact:

Right triangle fall through other shapes

Post by cyiss »

I am not sure what's going wrong with Right Triangle.
Or if it only happens on my machine.

The more a shape looks like a right triangle, the more possible it will fall through other shapes.

eg.

Code: Select all

    CGPoint verts[] = {
        cpv(-40.0f,-20.0f),
        cpv(0.0f,20.0f),
        cpv(40.0f,-20.0f),
    };
This is a right triangle.
A shape like this will easily fall through a segment shape or other shapes.

but when I change it a little bit, as

Code: Select all

    CGPoint verts[] = {
        cpv(-70.0f,-40.0f),
        cpv(0.0f,20.0f),
        cpv(40.0f,-20.0f),
    };
This is not a right triangle, but nearly right.
The same problem happens.

Any one who have a solution ,please let me know.
I will really appreciate!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Right triangle fall through other shapes

Post by slembcke »

Chipmunk doesn't do swept collisions. This means a fast moving object can miss collisions with other objects if the step size you are passing to cpSpaceStep() is too large. I don't think there is anything special about a right triangle that makes it more likely to miss collisions like that other than it's somewhat narrow shape. There are a few ways you can fix this.

Change your timestep: Before you do anything else, make sure you are using a constant timestep! If you are passing the time elapsed since the last frame, DON'T DO IT! Chipmunk does work this way, but it's not recommended because a hiccup in your framerate can easily allow objects to pass through each other. If your game runs at 60pfs, pass 1/60 seconds to cpSpaceStep(). If you need a smaller timestep, you can substep -- run two 1/120 second timesteps or three 1/180 second timesteps etc. This uses more CPU time, but if you have fast moving objects it might be necessary. A better, but more difficult way is to separate your simulation and drawing loops. This article will tell you why it's a good idea and how to do it: http://gafferongames.com/game-physics/f ... -timestep/ I do that in my own games.

The other option you have is to make your objects thicker. Instead of plain line segments, you can give them a radius when you create them. The thicker your line segments are, the less likely objects are to pass through them. This is generally what I do. You should still at least be using a simple fixed timestep even if you thicken your objects.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
dieterweb
Posts: 176
Joined: Fri Feb 27, 2009 7:12 am
Location: Germany
Contact:

Re: Right triangle fall through other shapes

Post by dieterweb »

Just a note: You can get un-smooth behavior if you use the approach scott linked IF you your game AND physics should run at 60fps. To try that follow the linked approach and set the physics interval to 1/59 or 1/61. Physics will not called every draw circle now resulting in stuttering movements even when you are at steady 60 fps.
Visit our game Blog: [url]http://zombiesmash.gamedrs.com[/url] or follow us on twitter: [url]http://twitter.com/zombiesmash[/url]
cyiss
Posts: 2
Joined: Tue May 31, 2011 4:33 am
Contact:

Re: Right triangle fall through other shapes

Post by cyiss »

Thank you for your reply!

Sorry that I didn't say my problem more clearly.

I put several right triangles on the ground,
they don't move and I don't move them,
suddenly some triangles begin to overlap with the ground,
then fall through it.

if I don't use right triangle(or shapes like right triangle), but some normal triangles, the problem does not happen again.

your link is very helpful.
I will try the approach you specified up there.

Thank you very much!!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Right triangle fall through other shapes

Post by slembcke »

Hmm. Well it's possible that it's a bug then.

When you say:
they don't move and I don't move them,
suddenly some triangles begin to overlap with the ground,
then fall through it
It makes me think that you are probably passing the time since the last frame, and are getting hiccups in your framerate. If the OS decides to unschedule your process for something else you can sometimes end up missing a dozen frames every once in a while. This is exactly why the advice in that linked article is helpful. It allows your game to keep running at the same speed even if if misses frames. If your game normally runs at a steady 60fps without performance problems running a fixed number of steps every frame will work fine.
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 12 guests