java 3d旋转图片,Java的3D旋转与四元

I have this method for rotating points in 3D using quaternions, but it seems not to work properly:

public static ArrayList rotation3D(ArrayList points, double angle, int xi, int yi, int zi)

{

ArrayList newPoints = new ArrayList<>();

for (int i=0;i

{

float x_old = points.get(i);

float y_old = points.get(i+1);

float z_old = points.get(i+2);

double w = Math.cos(angle/2.0);

double x = xi*Math.sin(angle/2.0);

double y = yi*Math.sin(angle/2.0);

double z = zi*Math.sin(angle/2.0);

float x_new = (float) ((1 - 2*y*y -2*z*z)*x_old + (2*x*y + 2*w*z)*y_old + (2*x*z-2*w*y)*z_old);

float y_new = (float) ((2*x*y - 2*w*z)*x_old + (1 - 2*x*x - 2*z*z)*y_old + (2*y*z + 2*w*x)*z_old);

float z_new = (float) ((2*x*z + 2*w*y)*x_old + (2*y*z - 2*w*x)*y_old + (1 - 2*x*x - 2*y*y)*z_old);

newPoints.add(x_new);

newPoints.add(y_new);

newPoints.add(z_new);

}

return newPoints;

}

If i make this call rotation3D(list, Math.toRadians(90), 0, 1, 0); where points is (0,0,10), the output is (-10.0, 0.0, 2.220446E-15), but it should be (-10,0,0), right? Could someone take a look at my code and tell me if is there somethig wrong?

Here are 4 screens that represent the initial position of my object, and 3 rotations with -90 degrees (the object is not properly painted, that's a GL issue, that i will work on later):

OasZm.png

KKPWA.png

2fuGw.png

R6xPn.png

解决方案

I haven't studied the code but what you get from it is correct: Assuming a left-handed coordinate system, when you rotate the point (0,0,10) 90 degrees around the y-axis (i.e. (0,1,0)) you end up with (-10,0,0).

If your coordinate system is right-handed I think you have to reverse the sign of the angle.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值