好的,这是我的程序:

#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();
}


我的程序的重点是:让用户在单击鼠标时指定2条高速公路,然后让用户在每条高速公路上指定2条汽车。一旦放置了第二辆车,我需要让它们沿着各自的高速公路行驶,并且我需要检测是否发生碰撞。我尚未在碰撞部分工作,但我的问题是让汽车行驶。我将使用以下内容沿每条公路的坡度线平移汽车:

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和y2是我用来确定坡度的每条公路的点,并且x和y设置为1.0以开始。

我知道这段代码可以在不同的坡度上进行翻译,但是我的问题是我不确定如何将这部分放在代码中。我试图将其放置在drawCar函数中,但是会出现错误一旦我达到了代码的那一点...任何想法?

最佳答案

斜坡baaaaaad。使用the vector form进行插值。

10-06 01:57