Jump to content
  • Advertisement
Sign in to follow this  
Himo

ball physics

This topic is 5003 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

I'm making a pinball game in C# and I conjured up the following method for boucing of the ball of objects. The problem is: The collision detection doesn't work at certain angles(Very sharp ones < 15 degrees) and that it that the ball isn't affected by gravity yet. Anyone got any ideas for those problems? Below is my method, thanks :) Edit: To ease the search the active equation for moving = x-axis = x-axis + speed * Tan(angle in radians) y-axis = y-axis + speed on collision the active direction of the relevant speed is inverted. Where speed is in pixels/100ms (so 1 pixel/100ms = 10 m/s) Notes: Objects[] is an PictureBox array that holds all the object that are on the table(like bumps and blackholes) vspeed is the vertical speed hspeed is the horizontal speed pc is the ball(a PictureBox) angle is the angle in degrees radians is that translated into radians
public void mover_Tick(object sender, System.EventArgs e)
		{
			pc.Top+=vspeed;
			pc.Left+=(int)(hspeed*Math.Tan(radians));

			if((pc.Top<=0))//Top
				vspeed=(-vspeed);
			if((pc.Top + pc.Height + this.Top)>=(this.Bounds.Bottom))//Bottom
				vspeed=(-vspeed);
			if((pc.Left<=0))//Left
				hspeed=(-hspeed);
			if((pc.Left + pc.Width + this.Left)>=(this.Bounds.Right))//Right
				hspeed=(-hspeed);

			if(pc.Bounds.IntersectsWith(objects[0].Bounds))
			{
				if(pc.Bounds.Bottom>objects[0].Bounds.Top)
				{
					vspeed=(-vspeed);
					angle+=5;
					radians = angle * (Math.PI/180);
				}
				if(pc.Bounds.Top<objects[0].Bounds.Bottom)
				{
					hspeed=(-hspeed);
					angle-=15;
					radians = angle * (Math.PI/180);
				}
				if(pc.Bounds.Left<objects[0].Bounds.Right)
				{
					vspeed=(-vspeed);
					angle-=20;
					radians = angle * (Math.PI/180);
				}
				if(pc.Bounds.Right>objects[0].Bounds.Left)
				{
					vspeed=(-vspeed);
					angle+=10;
					radians = angle * (Math.PI/180);
				}
			}
		}

[Edited by - Himo on January 7, 2005 4:21:18 AM]

Share this post


Link to post
Share on other sites
Advertisement
I don't think you are really saving yourself anything and creating headaches by trying to keep the left/right up/down motion components seperate. Also special casing all of the collision code will end up being a nightmare. PCs are really fast these days and for particle physics there really is no excuse not to just do the full math. Even if it has to run on a cell phone, it would be worth doing it right. You create a robust system that handles physics properly and it will all just work. You can easily add forces like gravity, springs, bumpers, whatever.

My old article on Pool physics will give you all the math and formulae you need. It was set up for full 3D but it the pool table is really mostly 2D. Besides you might want full 3D for a wicked multi-level pinball game.

http://www.darwin3d.com/gdm1999.htm#gdm0999

Your basic move() function should be a numerical integrator (Euler will work fine for this app):

//2d or 3d vector math
ball.velocity += ball.acceleration * timestep;
ball.pos += ball.velocity * timestep;

A GetForces() style function would take things like gravity and add it to the ball's acceleration.

Then you just need a check collision function that detects hitting walls and objects and changes the direction of the ball appropriately. For pinball this is much easier than pool since there is usually just one ball.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!