Hi all.
I started working with openGL recently and have been able to successfully load 3d models and to render them with lighting effects. This is all done on Windows. I'm planning on porting this app to the PSP once I complete it. I would like to know if anyone has references or already-written code/classes for 3D collisions of spherical objects.
Thanks.
3D collisions
um... that's pretty trivial, here's some pseudocode just from memory:
Edit: gah phpbb is retarded. "You cannot make another post so soon after your last; please try again in a short while." - making another post so soon my ass, I'm editing my own damn post I just made.
Code: Select all
bool spheres_collide(vec3d pos1, vec3d pos2, float rad1, float rad2)
{
vec3d dist = pos2-pos1;
float dist_squared = dot(dist,dist);
return (dist_squared < sqr(rad1 + rad2));
}
Thanks for the snippet ufoz, but I think a part of my post didn't appear:
"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."
Thanks again.
"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."
Thanks again.
It's basically the same as in 2D, if you use vector math to do it. What you need is the ball direction before the hit and the normal of the wall or surface and you're ready. Calculate the dotproduct of both vectors plus the dot product (square length) of the direction vector, then the reflection vector is the normal - 2*(n.d) / (d.d)*d, where . is the dotproduct, n is the normal and d the direction vector, as well as * the (scalar) multiplication.tkondal wrote:Thanks for the snippet ufoz, but I think a part of my post didn't appear:
"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."
Thanks again.
The reflection vector is then your new direction vector (which you could again scale by a value < 1 to denote the effect of friction if your direction vector contains the movement speed as its length).
When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection.
This assumes that the wall to hit, or the second sphere are static (ie, immovable). If you want the results of an elastic collision, you need a little different formula and calculate the movement vector of both objects after the hit, which depend on the masses of the two objects. But this goes pretty much into a complete physics simulation. Read here for the formula of elastic collision if you need it though: http://en.wikipedia.org/wiki/Elastic_collision
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl
Thank you very much Raphael. I will try to implement it as you're suggesting.Raphael wrote:It's basically the same as in 2D, if you use vector math to do it. What you need is the ball direction before the hit and the normal of the wall or surface and you're ready. Calculate the dotproduct of both vectors plus the dot product (square length) of the direction vector, then the reflection vector is the normal - 2*(n.d) / (d.d)*d, where . is the dotproduct, n is the normal and d the direction vector, as well as * the (scalar) multiplication.tkondal wrote:Thanks for the snippet ufoz, but I think a part of my post didn't appear:
"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."
Thanks again.
The reflection vector is then your new direction vector (which you could again scale by a value < 1 to denote the effect of friction if your direction vector contains the movement speed as its length).
When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection.
This assumes that the wall to hit, or the second sphere are static (ie, immovable). If you want the results of an elastic collision, you need a little different formula and calculate the movement vector of both objects after the hit, which depend on the masses of the two objects. But this goes pretty much into a complete physics simulation. Read here for the formula of elastic collision if you need it though: http://en.wikipedia.org/wiki/Elastic_collision
That's right :D Thanks for the clear upJim wrote:Which is the same as the normalised vector between the spheres' centres.When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection
Jim
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl