资源简介
需要有opengl,如何配置opengl请自行google;
实现小球之间,小球与地面的碰撞检测,请参考NeHe Tutorial的lesson 30
效果视频请点击:https://www.youtube.com/watch?v=VbTT1CLldOY&feature=youtu.be
代码片段和文件信息
/* CSCI 6555 Computer Animation Assignemnt 3
Author: Fei XIE email: xiefei@gwmail.gwu.edu
11/20/2012
**/
#include “stdafx.h“
#include
#include
#define pi 3.14159265358979
// screen width and height
int screenWidth;
int screenHeight;
// user initialization
GLfloat Velocity_New[10][3];
GLfloat Velocity_Old[10][3]={{200}{-200}};;
GLfloat Position_New[10][3];
GLfloat Position_Old[10][3];
GLfloat TimeStep=0.02f;
static GLfloat M[16]={0}; //the matrix for glMatrixMult
GLfloat MR[10][16]; //the matrix for each ball
GLfloat acc[3]={0-1.00}; // acceleration of gravity
GLfloat e=0.3f; //coefficient of restitution
int BallNumber=10; //The number of the balls
GLfloat LocationArray[10][3]={{-3.0f6.0f2.1f}{9.0f0.5f2.0f}{4.0f7.0f2.1f}{4.3f6.0f2.5f}{3.0f6.0f2.0f}{5.0f9.0f2.1f}{4.0f11.0f2.1f}{4.0f10.0f2.5f}{0.0f6.0f2.0f}{0.0f7.3f2.5f}};
//normalise the vector
void Normalise(GLfloat InterArray[3]){
GLfloat m2 = InterArray [0] * InterArray [0] + InterArray [1] * InterArray [1] + InterArray [2] * InterArray [2];
if (m2!=0 && (fabs(m2-1.0f)>0.00001f)){
GLfloat m = sqrt(m2);
InterArray[1] /= m;
InterArray[2] /= m;
InterArray[0] /= m;
}
}
//Dot product of two vectors
GLfloat DotProduct (GLfloat Vector2[3]GLfloat Vector1[3]){
return Vector2[0]*Vector1[0]+Vector2[1]*Vector1[1]+Vector2[2]*Vector1[2];
}
//Compute the distances between two balls
GLfloat Distance (GLfloat Vector1[3]GLfloat Vector2[3]){
GLfloat Distance=sqrt((Vector1[0]-Vector2[0])*(Vector1[0]-Vector2[0])+(Vector1[1]-Vector2[1])*(Vector1[1]-Vector2[1])+(Vector1[2]-Vector2[2])*(Vector1[2]-Vector2[2]));
return Distance;
}
// user initialization
void init()
{
for (int j=0;j MR[j][0]=1.0f;
MR[j][5]=1.0f;
MR[j][10]=1.0f;
for (int i=0;i<3;i++){
MR[j][12+i]=LocationArray[j][i];
Position_Old[j][i]=MR[j][12+i];
}
MR[j][15]=1.0f;
}
}
// Detect whether the ball Num have collision with other balls methods are from NeHe‘s opengl tutorial-----lesson 30
void CollisionDetect(int Num){
for (int i=Num+1;i if (Distance(Position_Old[Num]Position_Old[i])<1.01){ //Distance between two balls less than the diameter means the two balls occours collision
GLfloat X_Axis[3];
for(int j=0;j<3;j++){
X_Axis[j]=Position_Old[i][j]-Position_Old[Num][j];
}
Normalise(X_Axis);
//
GLfloat Temp=DotProduct(X_AxisVelocity_Old[Num]);
GLfloat U1_X[3] U1_Y[3];
for(int j=0;j<3;j++){
U1_X[j]=Temp*X_Axis[j];
U1_Y[j]=Velocity_Old[Num][j]-U1_X[j];
}
//
for(int j=0;j<3;j++){
X_Axis[j]=Position_Old[Num][j]-Position_Old[i][j];
}
Normalise(X_Axis);
Temp=DotProduct(X_AxisVelocity_Old[i]);
GLfloat U2_X[3] U2_Y[3];
for(int j=0;j<3;j++){
U2_X[j]=Temp*X_Axis[j];
U2_Y[j]=Velocity_Old[i][j]-U2_X[j];
}
GLfloat V1_X[3]
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-11-23 12:41 Assignment3\
目录 0 2012-11-23 12:41 Assignment3\Assignment3\
文件 7384 2012-11-23 12:40 Assignment3\Assignment3\Assignment3.cpp
文件 4374 2012-11-23 12:39 Assignment3\Assignment3\Assignment3.vcxproj
文件 1335 2012-11-23 12:39 Assignment3\Assignment3\Assignment3.vcxproj.filters
文件 143 2012-11-23 12:39 Assignment3\Assignment3\Assignment3.vcxproj.user
目录 0 2012-11-23 12:40 Assignment3\Assignment3\Debug\
文件 406 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.exe.em
文件 472 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.exe.em
文件 381 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.exe.intermediate.manifest
文件 93 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.lastbuildstate
文件 3811 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.log
文件 51563 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.obj
文件 1179648 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3.pch
文件 212 2012-11-23 12:40 Assignment3\Assignment3\Debug\Assignment3_manifest.rc
文件 27148 2012-11-23 12:40 Assignment3\Assignment3\Debug\CL.read.1.tlog
文件 1352 2012-11-23 12:40 Assignment3\Assignment3\Debug\CL.write.1.tlog
文件 1794 2012-11-23 12:40 Assignment3\Assignment3\Debug\cl.command.1.tlog
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 2122 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 4304 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 1414 2012-11-23 12:40 Assignment3\Assignment3\Debug\li
文件 482 2012-11-23 12:40 Assignment3\Assignment3\Debug\mt.command.1.tlog
文件 586 2012-11-23 12:40 Assignment3\Assignment3\Debug\mt.read.1.tlog
文件 482 2012-11-23 12:40 Assignment3\Assignment3\Debug\mt.write.1.tlog
文件 716 2012-11-23 12:40 Assignment3\Assignment3\Debug\rc.command.1.tlog
............此处省略36个文件信息
- 上一篇:DVB-S2标准协议
- 下一篇:Word必备工具箱199505
评论
共有 条评论