Slick Forums

Discuss the Slick 2D Library
It is currently Fri Oct 19, 2018 3:22 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri May 09, 2008 3:40 pm 
Offline

Joined: Wed May 07, 2008 2:52 pm
Posts: 5
Hi,

first, thanks for this neat physics library ;)

I'm currently writing a Pong-like game. In order to simulate the ball's movement I use Phys2D.

There are certain behaviours of Phys2D that look like potential bugs to me:
1) The ball's angle of reflection sometimes is the opposite of what it should be.
2) The ball passes through objects if it is too fast.
3) Not all collisions are being reported.

A few more details:
There's no gravity, dampening or friction. The ball is a dynamic body whereas the rest is static (including the paddles). Restitution is 1. The ball's movement is only controlled by the world (except when it is reset after somebody scored).

1) If the ball hits the corners of a paddle, it sometimes gets reflected at an impossible angle. If the two frontal corners get hit, the ball usually takes off at the opposite angle it is supposed to travel. For example if it comes flying at the paddle from above and hits the lower frontal corner. It will then be reflected at the same angle it arrived, which shouldn't happen.

If the ball hits the upper or lower back corner, it behaves as if it had hit the straight back side of the paddle. Just as if it is ignoring the angled side of the paddle's polygon.

2) If the ball is too fast (faster than in the above version) it passes through objects, e.g. even if you hit a paddle in its center, the ball reports its position as off screen. This is connected to the second part of 1) I guess. Even at lower speeds, it can pass through sides of the polygon and reflect off the back.

3) Sometimes the world does not report collisions for the ball. After every world.step() I check contacts of the ball with world.getContacts(ball). In some occasions this does not report any collisions even if the ball just bounced off a wall. You can see this happen if the ball hits something and no sound plays. This is not due to a bug in my audio player, as in a newer version, I also display particle effects which do not appear when the collision is missed.

I've tried adjusting world parameters as far as iterations and step length are concerned but it didn't seem to help.

Are these bug or did I just use Phys2D wrong? I know it's hard to tell without looking at the code but maybe it sounds familiar ;)


Last edited by Ciardhubh on Thu Nov 20, 2008 10:56 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 8:49 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
These are all symptoms of having a step size too large or attempting to update the simulation in too big time deltas. The engine is an approximation and a discrete collision system, so if things move to fast and hence jump long distances in a single steps all bets are off.

It's also worth noting that slight errors in restitution etc are also compounded by a too large step size. The engine is only ever an approximation not physically perfect.

1) Wrong angles are normally down to the engine deciding that an edge has been hit before the corner and hence a flat response. What are you using for the paddle shapes?

2) See above. This will happen if the step size is too large.

3) Possibly a bug, but I've used it an awful lot and never seen the issue. Will investigate on the next run.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 10:46 am 
Offline

Joined: Wed May 07, 2008 2:52 pm
Posts: 5
kevglass wrote:
These are all symptoms of having a step size too large or attempting to update the simulation in too big time deltas. The engine is an approximation and a discrete collision system, so if things move to fast and hence jump long distances in a single steps all bets are off.

It's also worth noting that slight errors in restitution etc are also compounded by a too large step size. The engine is only ever an approximation not physically perfect.

1) Wrong angles are normally down to the engine deciding that an edge has been hit before the corner and hence a flat response. What are you using for the paddle shapes?

2) See above. This will happen if the step size is too large.

3) Possibly a bug, but I've used it an awful lot and never seen the issue. Will investigate on the next run.

Kev


Thanks for the reply.

Am I assuming correctly that the standard step length is 1/60th of a second? I've been using this because Phys2D's Javadoc says results would be unpredictable if you use anything else. I've tried to decrease it down to 1/300.0f. The weird reflections didn't go away.

What I'm doing is basically call world.step() after the game ran as long as one step would take. The code below is in my game loop. timeDelta is the time since the game loop has run last time. I add passed time to timeToRun and if it's big enough I call world.step(). Strangely enough, if I decrease the step to 1/120 and MILLISECONDS_PER_STEP to (1.0f / 120.0f * 1000.0f) the whole game slows down by the factor two. Is the step length really in fractions of a second or is it in an arbitrary non-linear unit?
Code:
        timeToRun += timeDelta;
        while (timeToRun > MILLISECONDS_PER_STEP) {
            world.step(1.0f / 120.0f);
            timeToRun -= MILLISECONDS_PER_STEP;
        }


1) Paddles are polygons. They are drawn using the same vertices reported by Phys2D via paddle.getShape().getVertices(...). If Phys2D uses a discrete system, I can see why this happens:
Image
The ball "misses" the angled side in front and hits the back. Doesn't that make the engine unreliable? You can never know whether an object will be hit or if another object passes through it.

I still cannot explain this, though:
Image
Doesn't make sense to me ;)

3) I've found the bug of the missed collisions, which was completely my fault by the way ;). If the framerate drops below the world's step length I run multiple .steps() to compensate. After running it multiple times, I check for collisions. This only sees collisions in the last step.


Last edited by Ciardhubh on Thu Nov 20, 2008 10:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 10:57 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
The second case is bouncing off the next edge along because it's considered closer.

The steps I was talking about is actually called "iterations", it's passed into the constructor of the World. Higher number should make it more reasonable.

However, you're right, if objects are thin and moving quickly the engine is unreliable for collisions. There are two options:

a) Don't use cases that arn't reliable.
b) Implement continuous collision detection (or pick up the new version of JBox2D) and accept any performance impact (it might be quicker :))

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 12:01 pm 
Offline

Joined: Wed May 07, 2008 2:52 pm
Posts: 5
kevglass wrote:
The second case is bouncing off the next edge along because it's considered closer.

Which edge are you talking about? As far as I see it, there is no edge that could cause this angle in the second drawing. Unless the ball would hit the lower edge at the exact same angle as the edge itself.

kevglass wrote:
The steps I was talking about is actually called "iterations", it's passed into the constructor of the World. Higher number should make it more reasonable.

However, you're right, if objects are thin and moving quickly the engine is unreliable for collisions. There are two options:

a) Don't use cases that arn't reliable.
b) Implement continuous collision detection (or pick up the new version of JBox2D) and accept any performance impact (it might be quicker :))

Kev


I went as high as 1000 iterations but it didn't change.

Some time ago I wrote a small test (several hundred bodies falling on a few slides) to see which one of the two engines I prefer. JBox2D was actually faster for a high body count but I don't like its unJava-ishness :D.

Another reason why I didn't choose JBox2D was because it caused problems with bodies penetrating or sinking into each other. That's the reason why I chose Phys2D in the first place.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 12:14 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Wierd, 1000 iterations should have prevented case 1 altogether. Case 2 is always going to possible. The polygon code has decided that the ball has hit the lower edge (assuming it's travelling along the vector of the line).

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 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:  
cron
Powered by phpBB® Forum Software © phpBB Group