Collision Detection
Im trying to find some simple sample code on collision detection. Ive been looking on the internet for some but there is f*** all out there.
Would anyone please be able to put up some sample code of a sphere colliding with a cone or something like that.
Havent had much luck over in the java programmers forum, please help.
im doing a 3d game. Lets say that i have a sphere and a cube. I move the sphere around the screen using the keyboard...
I dont want the sphere to go through the cube, so i need some collision detection..
Im just looking for some sample code to get started.
I cannot find even one line of code on the internet that looks like collision detection.
This tutorial handles collision detection (2D, but it's a start):
[url=http://www.planetalia.com/cursos/Java-Invaders/JAVA-INVADERS-20.tutorial]Writing a Space Invaders game in Java[/url]
And please stop cross posting aka flooding.
http://forum.java.sun.com/thread.jspa?threadID=681769
http://forum.java.sun.com/thread.jspa?threadID=681774
A very simple solution:
- surely, you have assigned a position (xy, xyz, it doesn't matter) to each object
- enclose each object in an imaginary sphere, and define its radius as an attribute of each object
- define a function distance(A, B), where A and B are two points with their 3D coordinates - for 2D the third square difference is not required. The formula, for 2D, would be (sorry for the horrible math display...)
__
/22 2
distance = \ / (A.x - B.x)+ (A.y - B.y)+ (A.z - B.z)
Now, given two objects, check if their distance is more than A.radius + B.radius:
- if ==, the objects are in contact
- if >, the objects are detached
- if <, crash! The objects collide.
Hope this helps...
@dm@
@dm@a at 2007-7-16 1:00:43 >

The last is a difference, not a sum:(A.z-B.z)In other words:difference of x'sdifference of y'sdifference of z'ssum all three previous resultssquare root of the sumSorry again, this is my last consecutive post.
@dm@a at 2007-7-16 1:00:43 >
