Changes

Jump to: navigation, search

10th Floor/programming

981 bytes added, 16:44, 8 October 2010
Collision: sphere to rectangle collision
=Collision=
*'''Sphere to Sphere collisionCollision '''
**Each sphere is described by an x and y co-ordinate located in the very center, as well as a radius.
**If the distance between each sphere is less than the sum of their radius's, then a collision has occurred.
'''Sphere to Sphere Collision '''
<pre style="display: inline-block;">
int collision_sphere (x_sphere1, y_sphere1, r_sphere1, x_sphere2, y_sphere2, r_sphere2){
}
</pre>
'''Sphere to Rectangle Collision'''
**Sphere is still describes as above. x, y, and radius.
**Rectangles described as x, y from bottom left corner. As well as width and height.
**If the value of the sphere's x and y is between the rectangles borders + the radius of the sphere, collision has occurred.
<pre style="display: inline-block;">
int collision_sphere_rect (x_sphere, y_sphere, r_sphere, x_rect, y_rect, w_rect, h_rect){
int collision = 0;
if ( (x_sphere > x_rect) && (x_sphere < (x_rect + w_rect) ){ //potential collision, sphere between rectangle borders on x axis
if ( (y_sphere > y_rect) && (y_sphere < (y_rect + h_rect) ){ //confirmed collision, sphere also between rectangle borders on y axis
collision = true;
}
}
return collsion;
}
</pre>
**that's the basic idea. Depending on the direction the sphere is moving you'll have to add or subtract the radius from the comparision, otherwise collision will only be true when half or more of the sphere is within the rectangle.

Navigation menu