1

Ok heres my program:

#include<windows.h>
#include<gl/gl.h>
#include<gl/glu.h>
#include<glut.h>
#include<math.h>
#include<stdlib.h>
#include <stdio.h>

float points[6][2];
int count=0;
int moving=0;
float test=1.0;
GLfloat x=1.0, y=1.0;
GLint windW=680,windH=500;

void myDisplay()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();

}

static void drawRoad(int a1, int b1, int a2, int b2){

    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex2f(a1, b1); glVertex2f(a2, b2); glVertex2f(a2, b2-40); glVertex2f(a1, b1-40);
    glEnd();

}

static void drawCar(int a, int b){


    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex2f(a-30, b); glVertex2f(a, b+20); glVertex2f(a+30, b); glVertex2f(a, b-20);
    glEnd();

}

static void myMouse(int button,int state,int x,int y)
{

    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN){
        points[count][0]=x;
        points[count][1]=windH-y;

        count = count+1;

    }
    if(count==2){
    drawRoad(0.0, points[0][1], points[1][0], points[1][1]);

    }
    if(count==4){

    drawRoad(points[2][0], points[2][1], 0.0, points[3][1]);

    }
    if(count==5){

    drawCar(points[4][0], points[4][1]);

    }
    if(count==6){

    drawCar(points[5][0], points[5][1]);
    moving=1;

    }

    glFlush();

}


void myInit()
{
    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(0.1,0.5,0.7);
    glPointSize(2.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluOrtho2D(0.0,640.0,0,500.0);

}


void main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(680,500);
    glutInitWindowPosition(10,10);
    glutCreateWindow("Assignment 3");

    myInit();
    glutMouseFunc(myMouse);
    glutDisplayFunc(myDisplay);

    glutMainLoop();
}

The point of my program: I let the user specify 2 highways on mouse clicks, then I let the user specify 2 cars on each highway. Once the second car is placed I need to have the cars move down their respective highway and I need to detect if theres a collision. Im not working on the collision part yet, but my issue is getting the cars to move. I'm going to use the following to translate the cars along the slope line of each highway:

float m = (y2 - Y1)/(float)(x2 - x1);
if (fabs(m) <= 1)  //if abs(slope) is between 0 and 1
{
    //decide how to draw the line (start at x1 or x2?)
    if (x1 <= x2)
    {
        x=x+0.5;
        y = (y + m);
    }
    else
    {
    x=x-0.5;
        y = (y - m);
    }
}
else  //if abs(slope) is > 1
{
    //decide how to draw the line (start at y1 or y2?)
    if (Y1 <= y2)
    {
    y=y+0.5;
        x = (x + (1/m));
    }
    else
    {
    y=y-0.5;
        x = (x - (1/m));
    }
}
glTranslatef(x, y, 0.0);

x1, y1, x2, and y2 are the points from each highway I'm using to determine the slope and x and y are set to 1.0 to start.

I know this peice of code works for translating on different slopes but my problem is I'm not really sure where I should put this part into my code..I was trying to place it in the drawCar functions but then I would get an error once I got to that point of the code...any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kristen
  • 443
  • 1
  • 12
  • 25

2 Answers2

0

Slopes baaaaaad. Use the vector form for interpolation.

genpfault
  • 51,148
  • 11
  • 85
  • 139
0

You should use the latter code block in myDisplay() just before calling drawcar() (or translating straightaway) with the new values for a and b from there. (Note that you should do this twice: once for each car)

Also, you should consider using some form of acceleration due to gravity.

KK.
  • 783
  • 8
  • 20