Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411430 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 04:14:19 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Effects of object precedence in collisions
Pages: [1]
Print
Author Topic: Effects of object precedence in collisions  (Read 546 times)
Quarry
Level 10
*****


View Profile
« on: December 11, 2014, 01:58:53 PM »


Exhibit A

Exhibit B



I've never in my life been able to resolve collisions properly, and I really have to figure it out now

The problem I'm having today is not the resolution code, I wrote two different scripts to resolve circle and line, and circle and rectangle collisions

The problem is that when I iterate through the colliders and the circle, and update the circle's position inside the resolution code (before resolving the next one) the older elements seem to be "pushing" the circle away from them

Both exhibits show the phenomenon of the "gap" forming

How do I fix this from occurring?

Code here, hastily written, don't comment on the style but ask questions on what is what

http://pastebin.com/dBG9CvJG Line

http://pastebin.com/4qQjdHHM Rectangle
Logged
jgrams
Level 3
***



View Profile
« Reply #1 on: December 11, 2014, 05:31:49 PM »

It happens because your code thinks the circle is going at max velocity when it's actually sitting still.

Here's your code instrumented to show the last amount that each line collision moved the circle. If you put the circle on top of the two lines and hold the down arrow, you'll see that they stabilize at (0.58, -1.02) and (-0.58, -3.78).

I didn't show vx and vy, but if I did, you'd see that they're (0, 4.8).

So every frame the velocity moves the circle by (0, 4.8). Then the first line pushes the circle up and right so it doesn't overlap.  Then the second line pushes it up and back left. Then the circle moves around until it finds a balance point where they all cancel out.

You'll notice that if you let go of the keys, the circle settles back down (as the velocity is killed by the "friction") until it's resting nicely on the lines.

Does that make sense at all?

To fix it...your best bet might be to use a Verlet integration step instead of the symplectic Euler step you're using now. The Verlet works from the current and previous positions, and doesn't store the velocity, so you tend to get less of these problems where the position and the velocity don't match up.

Code:
Euler:
    x = x + v*dt
    v = v + a*dt

Symplectic or semi-implicit Euler:
(uses the next velocity instead of the previous one,
which makes it conserve energy better)
    v = v + a*dt
    x = x + v*dt

Verlet:
    x = x + (x - old_x) + a*dt*dt

Hope that helps!
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic