Slope collision and openGL

This is a discussion on Slope collision and openGL within the C Programming forums, part of the General Programming Boards category; I've got a pinball machine (a very lame one, but the point is to make it work as an assignment). ...

  1. #1
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79

    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?
    Nothing to see here, move along...

  2. #2
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    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?)

  3. #3
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Quote Originally Posted by auralius View Post
    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.
    Nothing to see here, move along...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  5. #5
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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.
    Nothing to see here, move along...

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ayreon View Post
    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?

  7. #7
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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.
    Nothing to see here, move along...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).
    Attached Images Attached Images  

  9. #9
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Thanks for that. That might help .
    Nothing to see here, move along...

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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.
    Nothing to see here, move along...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with AABB Collision Detection.
    By Shamino in forum Game Programming
    Replies: 10
    Last Post: 03-30-2009, 08:00 PM
  2. Collision Detection in OpenGL
    By Krak in forum Game Programming
    Replies: 13
    Last Post: 03-02-2009, 10:09 AM
  3. Some collision handling fun
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 04-13-2008, 09:45 AM
  4. Replies: 16
    Last Post: 09-22-2006, 04:39 PM
  5. 2d Collision Detection in openGL
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 05-12-2005, 12:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21