[英]Rotation around 3d object on touch using min3d/Rajawali framework (android)
I am working on an algorithm for rotating the camera around a 3D object using the Min3d/Rajawali framework.我正在研究使用 Min3d/Rajawali 框架围绕 3D 对象旋转相机的算法。
With my implementation, the rotation around axis X is not working properly.在我的实现中,围绕 X 轴的旋转无法正常工作。 I think the method setLookAt()
is not working properly.我认为setLookAt()
方法工作不正常。
The problem:问题:
When I rotate the sphere vertically, I can't fully see it.当我垂直旋转球体时,我无法完全看到它。 For example, turning the planet Earth, I can not fully see the Antarctic, because the algorithm resets the camera down.例如,转动地球,我无法完全看到南极,因为算法将相机重置。
Is it possible to realize the camera rotation around an object without using the method "setLookAt"?是否可以在不使用“setLookAt”方法的情况下实现相机围绕对象旋转?
I have tried different solutions, but have not been able to get it working correctly.我尝试了不同的解决方案,但未能使其正常工作。
Below is my code:下面是我的代码:
initScene:
scene.camera().position.z = 90;
scene.camera().target = raketeOBJ.position();
onTouchEvent:
public boolean onTouchEvent(MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
xpos = me.getX();
ypos = me.getY();
return true;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
touchTurn = 0;
touchTurnUp = 0;
return true;
}
if (me.getAction() == MotionEvent.ACTION_MOVE) {
float xd = me.getX() - xpos;
float yd = me.getY() - ypos;
xpos = me.getX();
ypos = me.getY();
touchTurn = xd / -200f;
touchTurnUp = yd / -200f;
return true;
}
try {
Thread.sleep(15);
} catch (Exception e) {
}
return super.onTouchEvent(me);
}
UpdateScene:
if (touchTurn != 0) {
scene.camera().position.rotateY(touchTurn);
touchTurn = 0;
}
if (touchTurnUp != 0) {
scene.camera().position.rotateX(touchTurnUp);
touchTurnUp = 0;
}
Number3d target = scene.camera.target;
Number3d cp = scene.camera.position.clone();
// move position like target is (0,0,0)
cp.x -= target.x;
cp.y -= target.y;
cp.z -= target.z;
cp.roateX(angle);
// restore offset
cp.x += target.x;
cp.y += target.y;
cp.z += target.z;
scene.camera.position.setAllFrom(cp);
A bit late but in case anyone has trouble with that, like me.有点晚了,但以防万一有人遇到麻烦,比如我。
Rajawali offers a camera called ArcballCamera , which does exactly what you/we are trying to do. Rajawali 提供了一个名为ArcballCamera的相机,它完全可以完成您/我们正在尝试做的事情。
In your Renderer add the following:在您的渲染器中添加以下内容:
ArcballCamera arcball = new ArcballCamera(mContext, ((Activity)mContext).findViewById(R.id.contentView));
arcball.setTarget(mObjectGroup); //your 3D Object
arcball.setPosition(0,0,4); //optional
getCurrentScene().replaceAndSwitchCamera(getCurrentCamera(), arcball);
now you can rotate and zoom the object without a dozen lines of code.现在您无需十多行代码即可旋转和缩放对象。
setLookAt() needs to be called onDrawFrame if you want the camera to update regularly.如果您希望相机定期更新,则需要在 onDrawFrame 上调用 setLookAt()。 But you need to care more about creating a "RotateAroundAnimation".但是您需要更关心创建“RotateAroundAnimation”。 See here for more info: http://www.rozengain.com/blog/2012/03/26/rajawali-tutorial-12-animation-classes/请参阅此处了解更多信息: http : //www.rozengain.com/blog/2012/03/26/rajawali-tutorial-12-animation-classes/
You'll have to make yourAnimation.setTransformable3D(mCamera), and then it should control your main camera.你必须制作 yourAnimation.setTransformable3D(mCamera),然后它应该控制你的主相机。 I often use this methodology and then call the "yourAnimation.start()" on touch, or other external stimulus.我经常使用这种方法,然后在触摸或其他外部刺激时调用“yourAnimation.start()”。
This is my way to rotate 3d model according as x and y这是我根据 x 和 y 旋转 3d 模型的方法
in Render class在渲染类中
public boolean left, right;
public boolean up, down;
and in onDrawFrame并在 onDrawFrame
@Override
public void onDrawFrame(GL10 glUnused) {
super.onDrawFrame(glUnused);
// getCurrentCamera().setRotY(getCurrentCamera().getRotY() + 1);
if (left) {
getCurrentCamera().setRotX(getCurrentCamera().getRotX() - 1);
}
if (right) {
getCurrentCamera().setRotX(getCurrentCamera().getRotX() + 1);
}
if (up) {
getCurrentCamera().setRotY(getCurrentCamera().getRotY() - 1);
}
if (down) {
getCurrentCamera().setRotY(getCurrentCamera().getRotY() + 1);
}
}
and in MainActivity并在 MainActivity 中
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
xpos = event.getX();
ypos = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
xpos = -1;
ypos = -1;
mRender.left = false;
mRender.right = false;
mRender.up = false;
mRender.down = false;
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
xd = event.getX() - xpos;
yd = event.getY() - ypos;
xpos = event.getX();
ypos = event.getY();
if (xd < 0) {
mRender.up = true;
} else {
mRender.down = true;
}
if (yd < 0) {
mRender.left = true;
} else {
mRender.right = true;
}
}
return super.onTouchEvent(event);
}
by this way, i can rotate my model :)通过这种方式,我可以旋转我的模型:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.