C++ 如何在OpenGL中使用键盘从另一个类移动一个对象?
如果我的球体是一个类,如何在OpenGL中从键盘移动球体 主要内容: 球体类别:C++ 如何在OpenGL中使用键盘从另一个类移动一个对象?,c++,opengl,glut,opengl-1.x,C++,Opengl,Glut,Opengl 1.x,如果我的球体是一个类,如何在OpenGL中从键盘移动球体 主要内容: 球体类别: #include "glos.h" #include <glut.h> #include <math.h> class Sphere{ public: float x, y, z, r; bool testColision = false; Sphere(float x, float y, float z, float r){ this->
#include "glos.h"
#include <glut.h>
#include <math.h>
class Sphere{
public:
float x, y, z, r;
bool testColision = false;
Sphere(float x, float y, float z, float r){
this->x = x;
this->y = y;
this->z = z;
this->r = r;
}
bool colision(Sphere sfera){
float d = sqrt(pow(++x - ++sfera.x, 2) + pow(++y - ++sfera.y, 2) + pow(++z - ++sfera.z, 2));
if (d <= r + sfera.r){
return true;
}
else{
return false;
}
}
void draw(){
glPushMatrix();
glTranslatef(x, y, z);
glutSolidSphere(r, 100, 100);
glPopMatrix();
}
void keyboardown(int key, int x, int y) {
if (key == 'w')
this->x += x;
if (key == 'a')
this->x -= x;
if (key == 'd')
this->y += y;
if (key == 's')
this->y -= y;
glutPostRedisplay();
}
};
我尝试使用glutSpecialFuncsfera1.keyboardown或auxKeyFuncAUX_RIGHT,sphere1.MutaDreapta1移动它,但在这两种情况下我都无法。。。有人能告诉我怎么做吗?glut和aux真的应该是互操作的吗? 或者glutSpecialFunc是auxSpecialFunc的拼写错误-
无论如何,在某些系统上使用键盘功能通常会更好,Up/Down/Special正在做一些奇怪的事情。这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论。几分钟前,我没有评论的特权;-。。。。无论如何,为什么我的答案不混合aux和glut不是一个有效的答案?glut和aux真的应该是互操作的吗?看起来更像一个问题。不要把aux和glut混在一起,如果你是这么写的话,这会是一个更好的答案。您应该编辑您的答案,以便更明显地提供解决方案。谢谢。不:我从来没有尝试过混合glut和aux,所以我不会允许自己断言这样做是错误的,即使99%确定。因此,恰当的方法是指出,除非我错了,而且那个人知道他在做什么,那么问题可能是xxx。试想一下,你在这里得到的所有反对票都来自于那些理解程度比你低的人,或者是某种编码风格的阿亚图拉:-。这让我重新要求更谦虚和积极的行为。我想任何人都能认出答案,即使答案是肯定的。
#include "glos.h"
#include <glut.h>
#include <math.h>
class Sphere{
public:
float x, y, z, r;
bool testColision = false;
Sphere(float x, float y, float z, float r){
this->x = x;
this->y = y;
this->z = z;
this->r = r;
}
bool colision(Sphere sfera){
float d = sqrt(pow(++x - ++sfera.x, 2) + pow(++y - ++sfera.y, 2) + pow(++z - ++sfera.z, 2));
if (d <= r + sfera.r){
return true;
}
else{
return false;
}
}
void draw(){
glPushMatrix();
glTranslatef(x, y, z);
glutSolidSphere(r, 100, 100);
glPopMatrix();
}
void keyboardown(int key, int x, int y) {
if (key == 'w')
this->x += x;
if (key == 'a')
this->x -= x;
if (key == 'd')
this->y += y;
if (key == 's')
this->y -= y;
glutPostRedisplay();
}
};