glTranslated

Hi,

I’m using GLUquadricObj *quadric; to create a sphere that moves. I’ve used glTranslated(x,y,z) in order to make it move but am having trouble reversing direction. I’ve tried some variations of this code using x*-1 instead but still it doesn’t bounce and reverse.

Thanks for your help.

from Ball.cpp :

  
void Ball::update() {  
    x+=1;  
    y+=1;  
    z+=1;   
    glTranslated(x, y, z);  
              
}  
  
void Ball:: bounceX () {  
 x-=1;  
glTranslated(x,y,z);  
    
}  
void Ball::bounceY(){  
y-=1;  
   
glTranslated(x, y, z);  
    
     
}  
  
void Ball::bounceZ (){  
    z-=1;  
      
    glTranslated(x,y,z);   
}  
  

From test app.cpp:

  
if(ball.x - ball.rad < 0 || ball.x + ball.rad >= ofGetWidth()) {ball.bounceX();}  
    if( ball.y - ball.rad < 0 || ball.y + ball.rad >= ofGetHeight()){ ball.bounceY();}  
   if( ball.z >=ofGetHeight() || ball.z + ball.rad >= ofGetWidth()){ ball.bounceZ();}  
  
  

You’re just calling bounce on a single frame, but you’re trying to get it to decrement. Try reversing your velocity, not just decrementing it by one (since that only happens once when it hits the wall and doesnt continue to reverse afterwards). So in bounce x, try x= -x;

It may also be useful to look into breaking your information into points and velocities to better understand what is happening there.

That way you could be like:

if(pt.x +radius >=ofGetWidth){
pt.x = ofGetWidth()-radius; //set it right on the edge
velocity.x = -velocity.x;
velocity.x = ofClamp(velocity.x, -maxSpeed, maxSpeed)
}