Slick Forums

Discuss the Slick 2D Library
It is currently Fri Dec 06, 2019 11:42 pm

All times are UTC

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Bodies will never rest
PostPosted: Tue Sep 01, 2009 11:42 am 

Joined: Tue Sep 01, 2009 11:15 am
Posts: 2
Hey all!

I'm working on a game using Phys2d with great pleasure so far, but I've now run into a problem that gives me serious headaches.

In a simple test world, I set up a static box (a floor so to speak) and an object falling down on it.
While all works well without any restitution set on the bodies, it's rather boring without any bounciness.
However, as soon as I set a restitution on the bodies (1.f on the floor, 0.5f on the object, but I found the values to be completely irrelevant concerning this issue), it seems as if there never comes the point where the world considers the "bouncing" energy to be zero. In fact, after a while, it increases again and stays almost constant at something very low.

Enough, however, to make my engine update the pixel position and so the object swaps between two positions, constantly hopping 1 (and sometimes even 2 or 3, very random!) pixel up and down and up and down... and it will not stop.

I can't seem to fix this problem, only setting a restitution to zero does, and that, of course, kills the bouncing.

Here's what I've tried:
  • Setting the world damping to different values
    Doesn't change a thing. A low value will nullify the bounce eventually, but the popping still occurs!
  • Set the body's damping.
    Won't help, will only make the thing fall more slowly, or - if I set it too high - will start to fly away against my gravitiy...
  • Giving frictions to the bodies.
    The objects both have a dummy friction of 0.5. Didn't change a thing for me, also played with the friction and it won't help. Would have been too werid if it helped, anyway.
  • Using different gravity values, doing more world steps per frame.
    I use 200.f because it feels right with my 60fps. Each frame I do a world step by 1 / 60 time units. Playing with this hasn't changed a thing.
  • Altering the object's mass.
    Should not help but I tried it anyway, setting it to stuff between 0 and 1000, and it doesn't help.
  • Use enableRestingBodyDetection.
    Either I missed the purpose of that function or it doesn't work at all. At least it never had any effect at all for me, even if setting the tresholds to astronomical values bodies will never rest or anything.
  • Avoid rounding my position values for drawing.
    This would be an optical workaround but never a fix. The position still changes, but you can't really see it unless you look very close.
    I will refuse to use this workaround, as I'm rendering using OpenGL and using unround position values will result in sprite filtering, that looks crappy. :roll:

So, I believe I'm missing something very obvious, but since I took the demos as my reference and therefore practically based my world off of it (just with a different time stepping), I just can't see what.

When the bouncing energy gets low enough, I expect a body to not bounce at all after a while, not even minimally. It should simply rest and do nothing unless a new force acts on it.
I mean, if you let a pen fall down it won't continue to pop up and down from the floor a millimeter, it'll just lay there flat after bouncing once or maybe twice.

Maybe the gravity pulls it down causing a new little bounce? If that's the case, that would be a bug.

Thanks for any help in advance. :)

 Post subject:
PostPosted: Tue Sep 01, 2009 1:45 pm 
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
The way the phys2d discrete collision detection and integration interact causes these slightly bounces. The problem is that it can't make a determination between a body that is falling very slowly and a body that has been very slightly moved away from a penetration. These very slightly errors build on one another which causes the slight bouncing of static objects.

You can either:

- Run a lot of iterations to improve collision accuracy, this won't help in the long run but might get you round the problem for th game.
- Use the hacky solution in rendering.
- Enable resting body detection (as shown in the platformer example)
- Use JBox2D instead (probably the sensible option)


 Post subject:
PostPosted: Tue Sep 01, 2009 2:50 pm 

Joined: Tue Sep 01, 2009 11:15 am
Posts: 2
- Use JBox2D instead (probably the sensible option)

Aw, not so pessimistic there.

I'm happy that I've come from a native Box2D and OpenGL approach (damn that was nasty) to a sole-Java one, and now I'm not feeling like switching libraries again.
I'm pretty happy with Phys2d, it's very lightweight with the maybe 6 classes I'm using, and the Vector class has become a key entity in my sources. :P

- Enable resting body detection (as shown in the platformer example)

That's exactly what I thought to be the thing I was looking for, however I couldn't get it to work at all, no matter how high I set the tresholds.
I'll make sure to have a look into that platformer example, thanks for the hint!

 Post subject:
PostPosted: Mon May 31, 2010 7:42 pm 

Joined: Wed May 19, 2010 4:50 pm
Posts: 28
I'm having similar problems.

Does anyone have a very simple sample of resting body code?

I call body.setCanRest(), yet when I check the value of body.isResting(), when there has been *no input at all* (gravity is turned off and the only other bodies in the world are static ones), and check isResting(), I get false. I also call body.getEnergy() and find it to be zero from frame to frame -- and still, isResting() returns false.

Kev, does the dynamic resting bodies thing work at all?

Any help would be super nice, thanks.

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group