現在地 >> メニュー >> サンプルコード::OpenGL >> 反発係数


問題


初速15m/s,60度の角度で投げ上げた時、
その物体のバウンドの様子を描画せよ

その他条件:
 反発係数0.85
 床に着いたときの時間と距離を出力する。
 0〜60メートルの間でよい。

答え


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

}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます