へんしゅうちゅう

#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; //初速
double a;
double s;//斜面移動距離
double Yo;//初期位置
double V;
double V_o;
};
typedef struct object OBJCT;


void display();
void reshape(int w, int h);
void timer(int value);

void DRAW_XYZ();

inline void DRAW_SLOPE();

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;
double slope_x,slope_y;
inline void SET_OBJ_DATA()
{
#define START_Y 10
#define THETA 30
#define PI_OVER_180 0.0174532925
#define g 9.8 //(m/s*s)
#define Vo 30 // (m/s)
obj.a = sin(THETA*PI_OVER_180)* g;
obj.Yo = 10;
obj.V_o = Vo;

slope_y = obj.Yo;
slope_x = obj.Yo/tan(THETA*PI_OVER_180);

}


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;


obj.s = obj.V_o * t + 0.5 * obj.a * t*t;

obj.x = obj.s * cos(THETA*PI_OVER_180);

if(slope_x<obj.x/2)
{
obj.y = 2*obj.Yo;
}else
{
obj.y = obj.s * sin(THETA*PI_OVER_180);
}


/** 現在の速度計算 **/
obj.V = obj.V_o + obj.a * t;
obj.V_x = obj.V*cos(THETA*PI_OVER_180);
obj.V_y = obj.V*sin(THETA*PI_OVER_180);


if(obj.x < 80)
{

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

DRAW_SLOPE();

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glPushMatrix();
glTranslated(obj.x/2, obj.Yo - obj.y/2, 0);
glTranslated(0, 0.5, 0);
glutSolidSphere(0.5,20,20);
glPopMatrix();

glDisable(GL_DEPTH_TEST);
}
else
{
t=0;
obj.x = 0;
obj.y = 0;
}

glutSwapBuffers();
t +=0.01;



}


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

}


inline void DRAW_SLOPE()
{
glBegin(GL_LINES);
glColor3d(1,0,1);//z
glVertex3d(0,slope_y,0);
glVertex3d(slope_x,0,0);

glEnd();

}

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






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


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

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