Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Fast sliding sphere-sphere math collision [dbc, should work in pro]

Author
Message
Arkheii
20
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 11th Jan 2004 10:54 Edited at: 11th Jan 2004 10:59
LOL it's really easy to make sphere-sphere collision because all you have to do is calculate the distance, but making it slide is a different thing. This here's a sliding math collision example between spheres. Since it's math and not polygon collision it has pitfalls like "slipping" through a tight corner (between spheres... it happens!?!, hehe), inaccuracy (which can be made to look accurate by increasing the polycount of the spheres by using a custom model), doesn't handle scaled objects yet, etc. but this is great if the spheres would be invisible and are used only as collision spheres.

This isn't a function, it's worse than that. It's just an example, uncommented, so you have to figure it out for yourself. In a nutshell, what this does is if it detects a collision (distance is less than the sum of the radii of two spheres), it sends out a "pointer" object, which does the ff. in order:

-position itself at the location of the collision object (the one that should not "move")
-point itself towards the object that should move upon collision (the one that will "slide")
-move itself by the distance of the sum of the radii of the two spheres (What this does is estimate where the sliding object "should have been" if it went the same direction, but not colliding with the other sphere)
get the x,y,z position of the pointer object and replace the playerx,playery,playerz variables with it (The x,y,z positions of the pointer is the x,y,z positions of the sphere if it was to "slide" along the surface of the sphere)

You could play around with some variables if you want. In this example, there is one player object (the one that will slide) and 3 other sphere objects (which the player can slide on). Only the player object is checked for collision with the others though, but I'm sure you could modify it by adding another for loop somewhere. Oh, and for fun, one of the spheres is revolving around an axis, which shows that even a moving object would affect collision (really cool, just see it for yourself).

btw, WASD to move, mouse to look around.

Login to post a reply

Server time is: 2024-04-28 17:26:16
Your offset time is: 2024-04-28 17:26:16