편집 : 방금 일부 값 -switched-가 있음을 알 수 있습니다.간단한 충돌 감지?
저는 Windows 컴퓨터에서 OpenGL을 사용하고 C로 코딩하고 있으며, 실제로는 정말 기본적인 충돌 감지를 시도하고 있습니다. 나 또한 함수에 통과 아무것도없는 것처럼, 변화를 시도했습니다
int collision(int box[2][4]) {
int col;
int i;
for (i=0;i<2;i++){
col = 0;
printf("x1:%.0f y1:%.0f x2:%.0f y2:%.0f \n",colbox[i][0],colbox[i][1],colbox[i][2],colbox[i][3]);
printf("Ox:%.1f Oy:%.1f Oz:%.1f \n" , Ox,here,Oz);
if ((colbox[i][0] < Ox) && (Ox < colbox[i][2])&& (colbox[i][1] < Oz) && (colbox[i][3] > Oz))
return 1;
else
return 0;
}
}
외부 루프를 넣어 : 는 지금, 나는 점 황소가, 오즈는 두 상자 안에 있는지 확인하기 위해 노력하고있어 함수, 등 문제가 평등 검사와 것 같다?
내가하려고하는 것의 가장 낮은 단계는 점 (카메라)이 경계 (2D 배열에 보관 된 4 점의 집합) 내에 있는지 확인하는 것입니다. 포인트가 경계에 없다면 0을 반환하십시오.
이 방법이 효과가있는 경우, 한 영역, IE로 이동하면됩니다. 처음 나열된 경계 영역은 올바르게 반환되지만 두 번째 영역은 올바르게 반환되지 않습니다. 정말 도움이 경우
전체 코드 :
// Values
double Ox=0, Oz=0;
float Oy=1.0;
double asp=1; // Aspect ratio
double dim=20; // Size of world
double fov=55; // Field of View
double ph,th = 0;
// angle of rotation for the camera direction
float angle = 0.0;
// actual vector representing the camera's direction
float lx=0.0,lz=-1.0;
// the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0;
float deltaMove = 0;
double here;
int collide;
// {x1,y1,x2,y2}
double colbox[2][4] = {
{3,3,6,6},
{-1,-14,-3,-16}
};
//front-1 back-2 left-3 right-4
int collision(double box[2][4]) {
int i;
for (i=0;i<2;i++){
col = 0;
printf("x1:%.0f y1:%.0f x2:%.0f y2:%.0f \n",colbox[i][0],colbox[i][1],colbox[i][2],colbox[i][3]);
printf("Ox:%.1f Oy:%.1f Oz:%.1f \n" , Ox,here,Oz);
if ((colbox[i][0] < Ox) && (Ox < colbox[i][2])&& (colbox[i][1] < Oz) && (colbox[i][3] > Oz))
return 1;
else
return 0;
}
}
//test
static void ball(double x,double y,double z,double r)
{
// Save transformation
glPushMatrix();
// Offset, scale and rotate
glTranslated(x,y,z);
glScaled(r,r,r);
// White ball
glColor3f(1,1,1);
glutSolidSphere(1.0,16,16);
// Undo transofrmations
glPopMatrix();
}
static void square(double r) {
glPushMatrix();
glScaled(r,r,r);
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(-1,0,-1);
glVertex3f(1,0,-1);
glVertex3f(1,0,1);
glVertex3f(-1,0,1);
glEnd();
glPopMatrix();
}
//test
void computePos(float deltaMove) {
Ox += deltaMove * lx * 0.1f;
Oz += deltaMove * lz * 0.1f;
}
void computeDir(float deltaAngle) {
angle += deltaAngle;
lx = sin(angle);
lz = -cos(angle);
}
// display
void display() {
here = 2.0f*Oy;
if (deltaMove)
computePos(deltaMove);
if (deltaAngle)
computeDir(deltaAngle);
const double len=2.0;
// Erase the window and the depth buffer
glClearColor(0.3,0.5,1.0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Enable Z-buffering in OpenGL
glEnable(GL_DEPTH_TEST);
// Set perspective
glLoadIdentity();
gluLookAt(Ox,2,Oz, Ox+lx, here, Oz+lz, 0,1,0);
ball(5,5,5,2); ball(-2,0,-15,1);
square(15);
glColor3f(1,1,1);
glBegin(GL_LINES);
glVertex3d(0.0,0.0,0.0);
glVertex3d(len,0.0,0.0);
glVertex3d(0.0,0.0,0.0);
glVertex3d(0.0,len,0.0);
glVertex3d(0.0,0.0,0.0);
glVertex3d(0.0,0.0,len);
glEnd();
// Label axes
glRasterPos3d(len,0.0,0.0);
Print("X");
glRasterPos3d(0.0,len,0.0);
Print("Y");
glRasterPos3d(0.0,0.0,len);
Print("Z");
// Render the scene and make it visible
glColor3f(0,0,0);
glWindowPos2i(5,5);
Print("Ox:%.1f Oy:%.1f Oz:%.1f lx:%.1f lz:%.1f Collide:%d", Ox,here,Oz,lx,lz,collide);
ErrCheck("display");
glFlush();
glutSwapBuffers();
}
void key(unsigned char ch,int x,int y) {
// Exit on ESC
if (ch == 27)
exit(0);
// WASD controls
else if (ch == 'a' || ch == 'A')
deltaAngle = -0.05;
else if (ch == 'd' || ch == 'D')
deltaAngle = 0.05;
else if (ch == 'w' || ch == 'W') {
collide=collision(colbox);
deltaMove = 1; }
else if (ch == 's' || ch == 'S') {
collide=collision(colbox);
deltaMove = -1; }
else if ((ch == 'e' || ch == 'E') && here < 4)
Oy += 0.01;
else if ((ch == 'c' || ch == 'C') && here > .5)
Oy -= 0.01;
Project(fov,asp,dim);
glutPostRedisplay();
}
void reshape(int width,int height) {
// Ratio of the width to the height of the window
asp = (height>0) ? (double)width/height : 1;
// Set the viewport to the entire window
glViewport(0,0, width,height);
// Set projection
Project(fov,asp,dim);
}
void releaseKey(unsigned char ch, int x, int y) {
if (ch == 'a' || ch == 'A')
deltaAngle = 0;
else if (ch == 'd' || ch == 'D')
deltaAngle = 0;
else if (ch == 'w' || ch == 'W')
deltaMove = 0;
else if (ch == 's' || ch == 'S')
deltaMove = 0;
Project(fov,asp,dim);
glutPostRedisplay();
}
int main(int argc,char* argv[]) {
// Initialize GLUT
glutInit(&argc,argv);
// Request double buffered, true color window with Z buffering at 600x600
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800,500);
glutCreateWindow("Haunted House");
// Set callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// glutSpecialFunc(special);
glutKeyboardFunc(key);
glutKeyboardUpFunc(releaseKey);
// Pass control to GLUT so it can interact with the user
ErrCheck("init");
glutMainLoop();
return 0;
}
이 상자는 X 축과 Y 축에 정렬되어 있습니까? – argentage
기술적으로 X 축과 Z 축에 정렬되었지만 3D로 탐지를 수행하지 않기 때문에 X와 Y로 생각합니다. –
다차원 배열 설정을 이해하려고하는데 실제로는 그렇지 않습니다. 무슨 일이 일어나고 있는지 알아낼 수 있습니다 - 특히 황소와 오즈. JS에서 이와 같은 코드를 몇 개 작성했지만 어레이의 이름 지정 체계와 그 기능이 다르게 보입니다. – argentage