Advertisement

Collision problems again

Started by October 29, 2002 06:24 PM
8 comments, last by endo 22 years, 3 months ago
I have got written some function for sphere/polygon collisions based on things I''ve seen on the web, no bounding boxes or anything so its reasonably simple stuff. I have a problem in using the functions in that sometimes the sphere collides more than once with the polygon which causes the sphere to pass through the polygon on occasions. Very bad! Anyone know what might be causing this and why? Or how to fix it maybe?? I''d post some code but there''s quite a lot and it wont all be relevant, so if you want to see something please ask. Thanks in advance
I thought I'd post the declarations of the collision functions I'm using so you can get an idea of what I'm trying to do


    #ifndef COLLISIONS_H#define COLLISIONS_H#include <GL/glut.h>#include "polygon.h"#include "vector3d.h"const double PI = 3.1415926535897932;const enum SPHERE_POSITION{	BEHIND,	INTERSECTS,	IN_FRONT};/*****************Plane-line collision functions*******************///distance of a plane from origin, normal & point are on planefloat planeDistance( const Vector3d& normal, const Vector3d& point );//tests the intersection between a triangle on the plane and a linebool intersectPlane( const Vector3d* poly, const Vector3d* line, Vector3d normal, float& originDistance );//overloaded to take a polygon objectbool intersectPlane( MyPolygon poly, const Vector3d* line, Vector3d normal, float& originDistance );/****************Polygon-Line collision functions******************///returns angle in radians, vectors must be normaliseddouble angleBetweenVectors( const Vector3d& normalisedVector1, const Vector3d& normalisedVector2 );//returns the point of intersection, assuming there is one!Vector3d intersectionPoint( Vector3d normal, const Vector3d* line, const float& distance );//returns true/false if the intersection point is inside the polygonbool insidePolygon( const Vector3d& intersection, const Vector3d* poly, const long vertexCount );//THE ray/polygon test functionbool intersectPolygon( const Vector3d* poly, const Vector3d* line, const int vertexCount );//overloaded ray/polygon test functionbool intersectPolygon( MyPolygon poly, const Vector3d* line );/****************Sphere-Polygon collision functions****************///returns the absolute value, ie without a signfloat absolute( float number );//uses pythagoras to get the distance between 2 3d points - SAME AS LENGTH CALCULATIONfloat distanceBetweenPoints( Vector3d point1, Vector3d point2 );//returns the point on line closest to point argumentVector3d closestPointOnLine( Vector3d& lineStart, Vector3d& lineEnd, Vector3d& point );//classifies a sphere according to its postion relative to the planeSPHERE_POSITION classifySphere( Vector3d& centre, Vector3d& planeNormal, Vector3d& pointOnPlane, float radius, float &distance );//hits the edge only, not an inside/outside testbool edgeSphereCollision( Vector3d& centre, Vector3d* poly, int vertexCount, float radius );//THE function to test for sphere/polygon collisionsbool spherePolygonCollision( Vector3d* poly, int vertexCount, Vector3d& centre, float radius );//overloaded for MyPolygon objectbool spherePolygonCollision( MyPolygon poly, Vector3d& centre, float radius );#endif		//COLLISIONS_H    


Edit : Sorry for the dodgy formatting

[edited by - endo on October 30, 2002 7:19:48 AM]
Advertisement
Can nobody help me?? I can send you the whole project so far if necessary...
It''s rare to find someone that will pour over your code to find the bugs in it for you. Having had a quick glance at what you''ve got there... you are only providing function declarations... so how is that going to help us find your bugs???

You are more likely to get a useful answer if you write out (generally in psuedo-code) what your algorithm is doing. That way people can point out flaws more easily.

Cheers,

Timkin
I wasn''t asking for someone to pour over the code and find me a perfect answer, I posted that code so that people could understand the functions I was using for the collision detection.

I am really looking for ideas as to why the sphere might occasionally bounce straight through polygons. Its not a very regular occurence with only 4 polygons being tested, but as I add more and more it becomes a real problem.

I was beginning to think it might be to do with the movement of the ball, but that is normal frame-independent movement. The collisions are tested inside the idle function so could that be the problem?

Here is the ball movement code in case anyone can spot a likely cause


  void Breakout::run( ){	fRate.setSpeedFactor( );		//use fRate.speedfactor to modify all movement//	cout << "FPS : " << fRate.fps << endl;		if( ball.isActive( ) )	{		ball.move( fRate.speedFactor );		testCollisions( );	}}//code to move the ballvoid Ball::move( GLfloat timeBasedModifier ){	location += speed * direction * timeBasedModifier;}  
If you are testing if the polygons'' vertices are inside the sphere, it might fail if the sphere passes through the center of the polygon.
Advertisement
I havn''t done much in the way of collision detection and reaction. But I know about the following problem which could be a reason why.

The sphere is moving too fast so that at time t-1 (last time you checked for collision) it is one side of a object and at time t (this round of collision checking) it is on the other side. At neither point in time is the sphere colliding with an object. This is a common problem with collision detection. Try reading up some of the gamedev.net articles on this problem.
BTW are you just detecting collisions or are you clipping as well?
I am testing to see if the sphere hits the polygon, not if the polygons vertices are inside the sphere. At the moment all I have is a sphere bouncing around a small box inside a window, the box being created from 4 polygons.

In fact I seemed to have the same problem before when I was just making the sphere bounce off the sides of the window, when no polygons were present and none of the collision functions in my other post were used. So the problem must lie in the ball movement right?? I just dont exactly where the problem is and how to correct it...

I have also noticed the ball tends to disappear if the window is moved. Not really sure why this might be, but it vould be a symptom of the same problem

This topic is closed to new replies.

Advertisement