最終更新: mikk_ni3_92 2007年10月26日(金) 12:50:01履歴
現在地 >> メニュー >> サンプルコード::OpenGL >> 反発係数
#include <iostream>
#include <cmath>
#include <cstdio>
#include <GL/glut.h>
struct object
{
double x,y;
double V_x,V_y;
double Vo_x,Vo_y;
};
typedef struct object OBJCT;
void display();
void reshape(int w, int h);
void timer(int value);
void DRAW_XYZ();
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(1,timer,0);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
OBJCT obj;
inline void SET_OBJ_DATA()
{
#define Vo 15
#define g 9.8
#define theta 60
#define PI_OVER_180 0.0174532925
obj.Vo_x = Vo * cos(theta*PI_OVER_180);
obj.Vo_y = Vo * sin(theta*PI_OVER_180);
obj.V_x = obj.Vo_x;
obj.V_y = obj.Vo_y;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
SET_OBJ_DATA();
glutMainLoop();
return 0;
}
/********[ここからコールバック]****************************************/
void display()
{
static double t = 0;
static double t_y=0;
if(!(obj.x > 0 && obj.y < 0))
{
obj.x = obj.V_x * t;
obj.y = obj.V_y * t_y - 0.5*g*t_y*t_y;
if(obj.y<0)
{
#define e 0.85
printf("[t : %f], [x : %f]\n",t,obj.x);
obj.V_y = - e*obj.V_y;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(30.0, 30.0, 60.0,15, 10.0, 0.0, 0.0, 1.0, 0.0);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslated(obj.x/1.5,obj.y/1.5,0);
glutSolidSphere(0.5,20,20);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
t +=0.1;
t_y +=0.1;
}else
{
if(obj.x > 60)
{
t = 0;
obj.V_y = obj.Vo_y;
puts("----------------------");
}
t_y = 0;
obj.x=0, obj.y=0;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
void DRAW_XYZ()
{
glBegin(GL_LINES);
glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);
glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);
glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();
}
#include <iostream>
#include <cmath>
#include <cstdio>
#include <GL/glut.h>
struct object
{
double x,y;
double V_x,V_y;
double Vo_x,Vo_y;
};
typedef struct object OBJCT;
void display();
void reshape(int w, int h);
void timer(int value);
void DRAW_XYZ();
using namespace std;
inline void GLUT_INIT()
{
glutInitDisplayMode(GLUT_RGBA| GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(200,200);
}
inline void GLUT_CALL_FUNC()
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(1,timer,0);
}
inline void MY_INIT()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
}
OBJCT obj;
inline void SET_OBJ_DATA()
{
#define Vo 15
#define g 9.8
#define theta 60
#define PI_OVER_180 0.0174532925
obj.Vo_x = Vo * cos(theta*PI_OVER_180);
obj.Vo_y = Vo * sin(theta*PI_OVER_180);
obj.V_x = obj.Vo_x;
obj.V_y = obj.Vo_y;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
GLUT_INIT();
glutCreateWindow("window name");
GLUT_CALL_FUNC();
MY_INIT();
SET_OBJ_DATA();
glutMainLoop();
return 0;
}
/********[ここからコールバック]****************************************/
void display()
{
static double t = 0;
static double t_y=0;
if(!(obj.x > 0 && obj.y < 0))
{
obj.x = obj.V_x * t;
obj.y = obj.V_y * t_y - 0.5*g*t_y*t_y;
if(obj.y<0)
{
#define e 0.85
printf("[t : %f], [x : %f]\n",t,obj.x);
obj.V_y = - e*obj.V_y;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(30.0, 30.0, 60.0,15, 10.0, 0.0, 0.0, 1.0, 0.0);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
DRAW_XYZ();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPushMatrix();
glTranslated(obj.x/1.5,obj.y/1.5,0);
glutSolidSphere(0.5,20,20);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
t +=0.1;
t_y +=0.1;
}else
{
if(obj.x > 60)
{
t = 0;
obj.V_y = obj.Vo_y;
puts("----------------------");
}
t_y = 0;
obj.x=0, obj.y=0;
}
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(1,timer,0); //タイマー関数
}
void DRAW_XYZ()
{
glBegin(GL_LINES);
glColor3d(0,1,0);//x
glVertex2d(-100,0);
glVertex2d(100, 0);
glColor3d(1,0,0);//y
glVertex2d(0,0);
glVertex2d(0,100);
glColor3d(0,0,1);//z
glVertex3d(0,0,-100);
glVertex3d(0,0, 100);
glEnd();
}