# ball physics

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

## 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;

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;
}
if(pc.Bounds.Top<objects[0].Bounds.Bottom)
{
hspeed=(-hspeed);
angle-=15;
}
if(pc.Bounds.Left<objects[0].Bounds.Right)
{
vspeed=(-vspeed);
angle-=20;
}
if(pc.Bounds.Right>objects[0].Bounds.Left)
{
vspeed=(-vspeed);
angle+=10;
}
}
}


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

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013217
×