# Slope collision and openGL

• 08-12-2009
Ayreon
Slope collision and openGL
I've got a pinball machine (a very lame one, but the point is to make it work as an assignment). I made the collisions with the sides and the top arch work, but i'm having trouble with two slopes at the bottom of the machine.

No matter how fast the ball comes in, the ball just sticks to the slope and slides along.
How can I make it bounce, like it does when it hits a vertical wall?

This is the piece of collision code:
Code:

``` if ( ypos <= (12.0+tan(alpha)*(xpos-33.0)) + R_ball/cos(alpha) & some other conditions )     {     phi = 0.5*M_PI + alpha;     theta = phi + atan(vx/vy) - 0.5*M_PI;     cosphi = cos(phi);     sinphi = sin(phi);     costheta = cos(theta);     v = sqrt( pow(vx, 2) + pow(vy, 2) );     vy = vy + 1.0*v*costheta*sinphi;     vx = vx + 1.0*v*costheta*cosphi;     }```
(12.0+tan(alpha)*(xpos-33.0)) describes the line of the slope, so if y is smaller or equal to this value, the ball must change its speeds and direction, that's what the vy= and vx= do.

The way i read it now, it should work, but it doesnt. Does anyone know why?
• 08-12-2009
auralius
how can we understand that algorithm?
my suggestion: use physics engine like: Box2D...you will make a very sophisticated game.
(you are using only x and y axis so it must be 2d game, right?)
• 08-12-2009
Ayreon
Quote:

Originally Posted by auralius
how can we understand that algorithm?

I'm sorry, I didn't think of that. I thought you could just assume all those angles and stuf are correct.
I think the problem lies in something else than the mathematics, since it will work when the ball has some incoming velocity and I give it an extra impuls on collision (i.e. not 1.0*v*costheta*sinphi, but 2.0*v*costheta*sinphi).

I can't use another program. This pinball machine was one of the end projects we could choose from, for a c-programming course. I'm afraid I'll have to do it in c.
• 08-12-2009
tabstop
So is the problem that this if should happen but doesn't, shouldn't happen but does, should happen and does happen but doesn't do what you want, or some combination thereof? If (3) what should it do and what does it do?
• 08-12-2009
Ayreon
This:
(12.0+tan(alpha)*(xpos-33.0)) + R_ball/cos(alpha)
Is a line of an inclined surface.

If the y position of the ball is < or = this line then:
add this to the x velocity component:
v*costheta*cosphi
and this to the y component:
v*costheta*sinphi

phi is the angle for a line perpendicular to the inclined surface, counted counter-clockwise, starting at zero at the horizontal plane.

theta is the angle of the incoming projectile measured from that line perpenicular to the inclined surface.

I kinda hoped the details weren't necessary, that someone simply recognised the problem and could come with a fast and easy solution :). That's why I didn't post any more.
• 08-12-2009
tabstop
Quote:

Originally Posted by Ayreon
This:
(12.0+tan(alpha)*(xpos-33.0)) + R_ball/cos(alpha)
Is a line of an inclined surface.

If the y position of the ball is < or = this line then:
add this to the x velocity component:
v*costheta*cosphi
and this to the y component:
v*costheta*sinphi

phi is the angle for a line perpendicular to the inclined surface, counted counter-clockwise, starting at zero at the horizontal plane.

theta is the angle of the incoming projectile measured from that line perpenicular to the inclined surface.

I kinda hoped the details weren't necessary, that someone simply recognised the problem and could come with a fast and easy solution :). That's why I didn't post any more.

That's a fairly clear interpretation of the code. What it completely and utterly lacks is any description of what the problem actually is. Is this section of code even being executed? Is it being executed but not doing what you want? What?

One thing: It appears from the code that theta is the angle between the velocity and the surface, not the angle and the normal?
• 08-12-2009
Ayreon
The ball is supposed to bounce of this line/surface. What it does, is slide along it, as if this surface attracts the ball. But, when the ball has a certain velocity, and I give the ball a little extra momentum, it WILL bounce off, also in the correct directions, so I assume all my angles are correct.
• 08-12-2009
tabstop
So I just traced your algorithm, arbitrarily choosing vx = 2, vy = -1, alpha = 1. The picture shows what I got, which is definitely scooting back up the wall. I would double check what it is you're trying to do (see comments about theta above).
• 08-12-2009
Ayreon
Thanks for that. That might help :).
• 08-12-2009
MK27
Why not just use something simple, like have it rebound at an inverse angle without changing velocity (which should be fairly realistic), and then tinker with that until you have the behaviour you want (eg to reflect the slight slope of a pinball table)?
• 08-12-2009
brewbuck
I'm not sure why you're using trigonometric functions here. The wall is a line segment. If the path of the ball intersects this line segment, then the velocity vector gets reflected across a ray which is normal to the wall segment and originates at the point of intersection -- none of this requires taking sines or cosines, it's pure vector projection.
• 08-13-2009
Ayreon
You know, i did have a feeling I was making things too complicated like this, but I didn't know another method. But I'll think about vector projection in the form of dot products and such, that might work.