Skip to main content
gamedev.net
🔒 Locked

Math question - Collision Detection Tutorial

Started by Moopheus Nov 19, 2003 at 1:40 PM 2 replies 900+ views
Original Post
Moopheus
Moopheus
Hallo! Fist here''s the tutorial I''m talking about: http://www.peroxide.dk/papers/collision/collision.pdf My question concerns the following part: " 3.4 The sweep test ... First we check if there is any time where the swept sphere collides against the infinite line going through the edge. This happens when the distance between the swept sphere center and the line is 1. Again this reduces to a quadratic equation with: " edge = p2 - p1 baseToVertex = p1 - basePoint A = ||edge||² * -||velocity||² + (edge*veolcity)² B = ||edge||² * 2*(velocity*baseToVertex) - 2*((edge*velocity)*(edge*baseToVertex)) C = ||edge||² * (1-||baseToVertex||²) + (edge*baseToVertex)² A*t² + B*t + C = 0 I want to know the "mathematical way" to get to this equations but I just can''t figure it out. Maybe someone here can help me
oliii
oliii
the way I see it for swept spheres vs segments is like this.

Doing a swept sphere vs segment collision test is equivalent to doing a ray vs a cylinder test. All you need, is to have the sphere centre at a distance of exactly the sphere radius.

The equation of a infinite cylinder is

((P - O) x H)^2 = R^2

O is a point on the cylinder core (say vertex V1 of segment)
H is the direction of the cylinder (say H = V2 - V1)


the ray equation for the sphere centre is

P = C + D.t

C is the centre of the sphere, D is either the displacement or the velocity of the sphere, it does not matter which way you do it.

plug in C + D.t in the infinite cylinder equation and you get

((C - O) x H + t.(D x H))^2 - R^2 = 0

it is an equation in the form

(X + t.Y)^2 - R^2 = 0

X = (C - O) x H
Y = (D x H)

expand the equation and you get

X^2 + t.(2.(X.Y)) + t^2.(Y^2) - R^2 = 0;

and this equation is in itself a second order equation in the form

a.t^2 + b.t + c = 0

where

a = Y^2 = (D x H)^2
b = 2.(X.Y) = 2 * (D x H) . ((C - O) x H)
c = X^2 - R^2 = ((C - O) x H)^2 - R^2


you can do the same system for two sphere colliding, or a ray vs. a sphere intersection (the tests are equivalent)

equation of the surface of a sphere : (P - O)^2 = R^2
Everything is better with Metal.
Moopheus
Moopheus
Thank you!

That helps me a lot
oliii
oliii
btw, you need to normalise the segment direction.

or ((P - O) x H)^2 = R^2 will not be meaningful.

say E is your un-normalised direction (E = V2 - V1), while H is the direction of the segment, and as length of 1.

E = H . |H|


H = E / |H|

(P - O) x H = (P - O) x (E/|H|) = ((P - O) x E) / |H|

so

((P - O) x H)² = ((P - O) x E)² / |H|²

and H.H = |H|²

so ((P - O)xH)² = ((P - O) x (V2 - V1))² / (V2 - V1)²

so in the end, if you have a segment [V1, V2], the equation for the capped cylinder around [V1, V2] is

((P - O) x (V2 - V1))² / (V2 - V1)² = R²

=> ((P - O) x (V2 - V1))² = R² . (V2 - V1)²
--------------------------------------------

plug in your ray equation in there, solve second order equation, there you go
Everything is better with Metal.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.