好的,这是我的程序:
#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用于插值。
关于c++ - 在不同斜坡上平移 openGL 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9536211/