Original Post
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 [Edited by - Himo on January 7, 2005 4:21:18 AM]
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);
}
}
}