Anything more complex will deal with calculations against the surface normal of the wall at the impact point.
- Splat
Anything more complex will deal with calculations against the surface normal of the wall at the impact point.
- Splat
I want an algorithm that will respond to the ball hitting the paddle or targets on the middle of the board. And I want it to be realistic. For example, what do I do when the ball hits the extreme left half of the rectangular paddle, I don't think you can just inverse the x and y velocities. Please include some code samples.
+------------+| |+------------+
+--+------+--+| | | |+--+------+--+
I am assuming that is what you want right?
And with this example you can split it up into more than three sections and use as many as you want.
What do you think Splat & mmarvi???
------------------
-Dom
Visit - http://www.eisa.net.au/~sdgrab/index.html
[This message has been edited by Dæmin (edited December 01, 1999).]
- Splat
Thanks for your advice, it sounds like a good idea. However, at what angle should I deflect the ball when it hits the "side paddles", i.e. not the middle paddle? If possible, include some code samples.
By using a degree-based direction on the "ball" (G, don't even know the english word on the hockey thing, so i'll use ball instead,ok?), the bounce algorithm is quite easy.
Suppose you put a huge cylinder on the ice, and a ball bounces towards it:
//Ball movement algorightm
ball.x = cos(ball.dir)*ball.speed;
ball.y = sin(ball.dir)*ball.speed;
//a funcion that determines if the ball is //"inside" the cylinder
if (In_Cylinder(ball.x,ball.y))
{
//Check the angle at the point on the //cylinder where the ball boundces
float cylinderdir = atan(ball.y/ball.x);
//The difference between the ball direction //and the cylinder point direction
float dirchange = ball.dir-cylinder.dir;
//Set the new angle
ball.dir = cylinder.dir-dirchange;
//This whole example may be complemented //with some "%"-functions also
}
there.
The way i see it, the main problem is finding a good algorithm for the
InCylinder() - function. It's easy making this function, but hard to make it go fast.
Please do not use this specific example in your program. I just got a brainstorm while reading this post, and i thought it would be something to think of.
I.e.
1) / <- Incoming vector /+--+----+--+2) \ <- Outgoing vector \+--+----+--+3) +45 degrees right \ | <- Final vector (roughly) \|+--+----+--+
Do you roughly get what I mean?
And if you wanted to add more segments then you could alter the angles that are added(taken away in some cases) to get a different effect.
------------------
-Dom
Visit - http://www.eisa.net.au/~sdgrab/index.html
[This message has been edited by Dæmin (edited December 02, 1999).]
1. calculate the angle in which the linesegment goes.
it doesnt matter if you go from x1,y1 to x2,y2 or the other way. it JUST DOESNT MATTER.
dont use atan, use atan2, to avoid division by 0
surfaceangle=atan2(x2-x1,y2-y1)
2. get the normal of the surface angle
surfacenormalangle=surfaceangle+pi/2
(you can also subtract pi/2, it doesnt really matter)
3. get the incoming angle of the ball
again, using atan2
incomingangle=atan2(x,y)
//x and y are the x and y components of the balls velocity
4. reverse the incoming angle(we are going to be reflecting this angle across the normal angle, so we need it outgoing)
incomingangle+=pi
(or -=pi if you like)
5. calculate the outgoing angle
outgoingangle=incomingangle+(surfacenormalangle-incomingangle)*2
6. adjust the x,y of the ball's speed
mag=sqrt(x*x+y*y)
x=mag*cos(outgoingangle)
y=mag*sin(outgoingangle)
Get off my lawn!
Thanks for your algorithm. However, I'm a little confused about what the variables represent. Will this particular algorithm slow down the game any? Do you have any code samples on this algorithm?