 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
tkondal
Joined: 25 May 2006 Posts: 8
|
Posted: Tue Oct 17, 2006 8:47 am Post subject: 3D collisions |
|
|
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. |
|
| Back to top |
|
 |
ufoz
Joined: 10 Nov 2005 Posts: 86 Location: Tokyo
|
Posted: Tue Oct 17, 2006 11:34 am Post subject: |
|
|
um... that's pretty trivial, here's some pseudocode just from memory:
| Code: |
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));
}
|
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. |
|
| Back to top |
|
 |
tkondal
Joined: 25 May 2006 Posts: 8
|
Posted: Tue Oct 17, 2006 11:15 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Wed Oct 18, 2006 12:28 am Post subject: |
|
|
| 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. |
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.
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 |
|
| Back to top |
|
 |
tkondal
Joined: 25 May 2006 Posts: 8
|
Posted: Wed Oct 18, 2006 5:37 am Post subject: |
|
|
| Raphael wrote: | | 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. |
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.
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 |
Thank you very much Raphael. I will try to implement it as you're suggesting. |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Wed Oct 18, 2006 8:00 am Post subject: |
|
|
| Quote: | | When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection |
Which is the same as the normalised vector between the spheres' centres.
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Raphael

Joined: 17 Jan 2006 Posts: 646 Location: Germany
|
Posted: Wed Oct 18, 2006 9:07 am Post subject: |
|
|
| Jim wrote: | | Quote: | | When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection |
Which is the same as the normalised vector between the spheres' centres.
Jim |
That's right :D Thanks for the clear up _________________ <Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki
Alexander Berl |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|