PDA

View Full Version : Angles and Collision Detection



DragonXTC
04-28-2002, 06:02 PM
Lets say i have a ball, and it hits a wall at a certian angle and i want it to leave the wall at the exact opposite angle. any clue how this would be achieved? i think that it is the tangent of the angle but im not really sure. Any help would be appreciated! thanx!

Omaha
04-28-2002, 06:22 PM
Tangent, no no no. You need to brush up on your trig.

If you think of an X axis lying along your surface of impact, with the Y axis going straight out:

NewAngle=180-OldAngle

Robbo
04-28-2002, 11:46 PM
You can work out the cosine of the angle by finding the dot product of the surface its hitting and the velocity vector.

The easiest way however, for a vertical wall and a ball moving with velocity V(x,y) is NV(-x, y). So the ball just changes direction. Same deal with a horizontal surface NV(x, -y).

Furrage
04-29-2002, 07:32 AM
Hmmm. That sounds similar to what was discussed in this post ( http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/008162.html ). Only difference is you want angles where we converted it to vectors.

nexusone
04-29-2002, 09:18 AM
Here is an example of how I am handling the bouncing off the wall's:




#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

static int player_score = 0;

static float paddle_pos = 125, paddle_state = 0, paddle_direction = 0;

static float ball_pos_x = 100, ball_pos_y = 100, ball_state = 1, ball_rate = 4, ball_direction_x = 1, ball_direction_y = 1, ball_angle = 90;

void init(void)
{

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);

}

void display(void)
{

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glMatrixMode(GL_MODELVIEW);

glBegin(GL_LINES); // draw border
glColor3f(1.0, 1.0, 1.0);
glVertex2f( 0.0, 0.0);
glVertex2f( 499.0, 0.0);
glVertex2f( 499.0, 0.0);
glVertex2f( 499.0, 249.0);
glVertex2f( 499.0, 249.0);
glVertex2f( 0.0, 249.0);
glVertex2f( 0.0, 249.0);
glVertex2f( 0.0, 0.0);
glEnd();


glPushMatrix(); // draw paddle
glColor3f( 1.0, 1.0, 1.0);
glTranslatef(20, paddle_pos, 0);
glScalef( 1.0, 4.0, 1.0);
glutSolidCube( 10 );
glPopMatrix();

glPushMatrix(); // draw ball
glTranslatef( ball_pos_x, ball_pos_y, -10);
glColor3f( 0.5, 0.5, 0.5);
glutSolidSphere(5, 16, 8);
glPopMatrix();

glColor3f( 1.0, 0.0, 0.0);
glRasterPos3i(5, 24, 0);
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'S');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'C');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'O');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'R');
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, 'E');


glutSwapBuffers();
}


void reshape (int w, int h)
{

if (( w != 500 ) &amp;#0124; &amp;#0124; ( h != 250 ))
{
glutReshapeWindow( 500, 250);
w = 500;
h = 250;
}

glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho(0.0f, (GLfloat) w,(GLfloat) h, 0.0f,-10.0f,10.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}


}

void Special_keys(unsigned char key, int x, int y)
{
switch (key) {
case GLUT_KEY_DOWN:
paddle_state = 2;
paddle_direction = 1;
break;
case GLUT_KEY_UP:
paddle_state = 2;
paddle_direction = -1;
break;
default:
break;
}

}


void My_event_manager(void)
{

if (paddle_state > 0) // Manage paddle if in motion
{
paddle_pos = paddle_pos + 0.5 * paddle_direction;
paddle_state--;
}

if (paddle_pos < 10) paddle_pos = 10;
if (paddle_pos > 240) paddle_pos = 240;

if (ball_state > 0) // Manage ball if in motion
{
ball_pos_x = ball_pos_x + 0.5 * ball_direction_x;
ball_pos_y = ball_pos_y + 0.5 * ball_direction_y;
}

if ((ball_pos_x > 490) &amp;#0124; &amp;#0124; (ball_pos_x < 10)) ball_direction_x = ball_direction_x * -1;
if ((ball_pos_y > 240) &amp;#0124; &amp;#0124; (ball_pos_y < 10)) ball_direction_y = ball_direction_y * -1;

if ((ball_pos_x < paddle_pos + 20) &amp;&amp; (ball_pos_x > paddle_pos - 20))
{
if ((ball_pos_y < 35 ) &amp;&amp; (ball_pos_y > 30))
{
ball_direction_x = 1;
ball_direction_y = 1;
}

if ((ball_pos_y < 20 ) &amp;&amp; (ball_pos_y > 25))
{
ball_direction_x = -1;
ball_direction_y = -1;
}
}

glutPostRedisplay();

}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 250);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle("Glutplanets");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(Special_keys);
glutIdleFunc(My_event_manager);
glutMainLoop();
return 0;
}




Originally posted by DragonXTC:
Lets say i have a ball, and it hits a wall at a certian angle and i want it to leave the wall at the exact opposite angle. any clue how this would be achieved? i think that it is the tangent of the angle but im not really sure. Any help would be appreciated! thanx!