ball going through the wall

Discuss any Chipmunk bugs here.
Post Reply
andrews
Posts: 16
Joined: Wed Apr 27, 2011 5:35 am
Contact:

ball going through the wall

Post by andrews »

What can be done in general to avoid a body going through a wall at high speed?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: ball going through the wall

Post by slembcke »

Doing some or all of these things will help:
  • Use a smaller timestep. Make the steps half as small and take two (or more) at a time, or decouple the game's frame rate with the simulation rate: http://gafferongames.com/game-physics/f ... -timestep/
  • Use a fixed (constant) timestep. Actually, you should do this anyway. Otherwise if your game's frame rate hiccups because the OS decides to do something in the background, you might get a single step that is several times longer than normal.
  • Make the walls thicker.
  • Make the ball move slower. You can force that by setting the body's velocity limit.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
andrews
Posts: 16
Joined: Wed Apr 27, 2011 5:35 am
Contact:

Re: ball going through the wall

Post by andrews »

Thanks! Very helpful details.
andrews
Posts: 16
Joined: Wed Apr 27, 2011 5:35 am
Contact:

Re: ball going through the wall

Post by andrews »

Thanks for you previous response, it helps me but still have some questions. I am still struggling trying to make the ball move fast and be responsive but not to go through the wall at the same time.

1. "Use a fixed (constant) timestep."
Is this what you meant? Using 1.0/60.0 instead of displayLink.duration*displayLink.frameInterval?

- (void)update
{
if ( !m_gameStopped )
{
// Step (simulate) the space based on the time since the last update.
cpFloat dt = 1.0/60.0;//displayLink.duration*displayLink.frameInterval;
[space step:dt];

// etc..

}
}

Changing the timestep changes how fast the ball moves.


2. "Make the steps half as small and take two (or more) at a time" -
so I could change 1.0/60.0 to 0.5/60.0 - half as small.
"take two or more at a time" - could you please explain?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: ball going through the wall

Post by slembcke »

Display links actually have a fixed interval, so that is OK to use if you want. Things are slowing down when you use a smaller timestep because you are advancing the simulation forward by say 1/120th of a second for each 1/60th of a real second. If you halve the timestep, you'll need to take two of them instead. The Chipmunk demos work this way, splitting each frame up into a number of physics steps.

A better, more robust way to do it is like this: http://gafferongames.com/game-physics/f ... -timestep/. A lot of simple games will get away just fine with the above however.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
andrews
Posts: 16
Joined: Wed Apr 27, 2011 5:35 am
Contact:

Re: ball going through the wall

Post by andrews »

Nice! I finally understood what you meant by "take two of them instead".
[space step: dt];
[space step: dt];

Thanks again. Seems to work now.

so instead of
cpFloat dt = 1.0/60.0;
[space step:dt];

is use:
cpFloat dt = 0.5/60.0; // same as 1.0/120.0
[space step:dt];
[space step:dt];
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests