Archived

This topic is now archived and is closed to further replies.

Having some problems with object collision, please help me =/

This topic is 5381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This has really been bugging the hell out of me for the last couple days. Here's my situation: I tried making a version of Slime Volleyball from http://diddly.com/slime and it's been going fine until I get to some object collision. Here's my code for detecting ball/slime collision (slimes are semicircles, the ball is a circle):
          
CBall::Update()
{
//Do some other collision checks


        //Slime check

	//If the ball's X value, fX, is less than zero, then

	//it is on the left side of the net check the left slime

if(fX<0.0f)
	{
		//Rough check of distance between slime/ball

		if(Distance(fX+fXVelocity*iTimeDifference,pSlime[0]->fX,fY+fYVelocity*iTimeDifference,pSlime[0]->fY)<=fRadius+pSlime[0]->fRadius)
		{
			//Get the angle in radians of the collision

			fAngle=atan((fY-pSlime[0]->fY)/(fX-pSlime[0]->fX));
			//Get the magnitude of the ball speed

			float fMag=Distance(fXVelocity/1.5f,0,fYVelocity,0);
		
			//Determine the ball's new velocity

			//X velocity is 1.5x Y velocity

			fXVelocity=1.5f*fMag*cos(fAngle);

			//I don't know why I have to check this, but it's the only way it works

			if(fAngle<0)
			{
				fXVelocity*=-1;
				fYVelocity=fMag*sin(-fAngle);
			}
			//Check when the ball is below the slime and will collide with the slime

			else if(fY+fRadius<pSlime[0]->fY && fX>pSlime[0]->fX-pSlime[0]->fRadius && 
				fX<pSlime[0]->fX+pSlime[0]->fRadius && 
				fY+fRadius+fYVelocity*iTimeDifference>pSlime[0]->fY+pSlime[0]->fVelocity)
			{
				//=================PROBLEM HERE================

				//This is the closest I can get it to working

				//=============================================


				//Set Y Velocity to a certain value because I can't get it to work otherwise

				fYVelocity=-0.005f;
				//fYVelocity=-abs(fYVelocity);  <--- this doesn't work

			}
			else
				//Set YVelocity

				fYVelocity=fMag*sin(fAngle);
		}
		else
			//No collision, so slow down the ball Y Velocity

//iTimeDifference is how much time elapses every frame

//0.000005f is the acceleration due to gravity

			fYVelocity-=float(iTimeDifference)*0.000005f;
	}
	else
        //Check other side

          
Alright, most of this works fine EXCEPT for the part where it checks if the ball is underneath the slime. I was forced to put in that forced YVelocity because I couldn't get it to work any other way. What happens if I don't do this is the ball just shoots up through the slime as if it had collided with the top part of the slime. Can anyone please help me? Here's a link in case you want to check it out: Slime Volleyball [edited by - Kaezin on March 25, 2003 5:10:10 PM]

Share this post


Link to post
Share on other sites