forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

3D collisions

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
tkondal



Joined: 25 May 2006
Posts: 8

PostPosted: Tue Oct 17, 2006 8:47 am    Post subject: 3D collisions Reply with quote

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
View user's profile Send private message
ufoz



Joined: 10 Nov 2005
Posts: 86
Location: Tokyo

PostPosted: Tue Oct 17, 2006 11:34 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website AIM Address MSN Messenger
tkondal



Joined: 25 May 2006
Posts: 8

PostPosted: Tue Oct 17, 2006 11:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Wed Oct 18, 2006 12:28 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
tkondal



Joined: 25 May 2006
Posts: 8

PostPosted: Wed Oct 18, 2006 5:37 am    Post subject: Reply with quote

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
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Wed Oct 18, 2006 8:00 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Wed Oct 18, 2006 9:07 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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