Animate bouncing box using?



我需要制作一个动画,其中框从(- 15,0)开始,然后上升到(- 8,15),当它到达那里时,然后下降。等等,就像一个DVD屏幕保护程序,但我似乎找不到它的工作公式。

代码如下:

#include<windows.h>
#include<GL/glu.h>
#include<GL/glut.h>
#ifdef APPLE
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<stdio.h>
#include<stdlib.h>
#include <GL/freeglut.h>
//Variable menampung sudut awal
float xpos = -15.0;
float deltax = 0.5;
float ypos = 0.0;
bool balik = true;
bool turun = true;
//Variable mengatur perubahan sudut
void myInit(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-15.0f, 15.0f, -15.0f, 15.0f);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-15.0, 0.0);
glVertex2f(15.0, 0.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-8.0, 15.0);
glVertex2f(-8.0, -15.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(8.0, 15.0);
glVertex2f(8.0, -15.0);
glEnd();
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(0.0, 15.0);
glVertex2f(0.0, -15.0);
glEnd();

glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(-0.2 + xpos, 0.2 + ypos);
glVertex2f(-0.2 + xpos, -0.2 + ypos);
glVertex2f(0.2 + xpos, -0.2 + ypos);
glVertex2f(0.2 + xpos, 0.2 + ypos);
glEnd();
glutSwapBuffers();
}
void Timer(int) {
glutPostRedisplay();
glutTimerFunc(100, Timer, 0);
if (ypos < 15 && xpos <-8 && turun == true){
xpos = xpos + deltax;
ypos = (2.143*xpos) + 32.143;
}
else {turun = false;};

if (ypos > -15 && xpos <8 && turun == false){
xpos = xpos + deltax;
ypos = -1,875*xpos + ;
}
else turun = true;

}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animasi");
glutDisplayFunc(myDisplay);
myInit();
glutTimerFunc(0, Timer, 0);
//Perulangan
glutMainLoop();
return 0;
}

如果你想让盒子反弹,你可以根据你击中的轴来反转x或y速度。如果你想让盒子随着重力反弹,你将使用如下的矢量参数方程:Y = -16t^2 + vsin(x) + s.y, x = vcos(x) + s.x,其中t是时间v是大小(速度)s是盒子的起始位置,每次与侧面碰撞都会重置。保持大小不变,无限反弹。如果方程令人困惑,请在网上查找vector pre-calculus。

最新更新