屏幕坐标转地面坐标的一种极简计算方式

没有使用物理的碰撞检测
onTouchStart(event: EventTouch) {
    let screenPoint = event.getLocation()

    const outRay = new ray();
    this.cameraCom.screenPointToRay(screenPoint.x, screenPoint.y, outRay);

    // (x-x0)/a=(y-y0)/b=(z-z0)/c
    // x = x0 + (y - y0) * a / b
    // z = z0 + (y - y0) * c / b
    let x = outRay.o.x + (0 - outRay.o.y) / outRay.d.y * outRay.d.x
    let z = outRay.o.z + (0 - outRay.o.y) / outRay.d.y * outRay.d.z

    this.cube.setPosition(x, 0, z)
}

demo translate.rar (6.1 KB) Creator 3D v1.1.0

15赞

:+1::+1::+1:

在 screenPointToRay 修复之前,我是用四元数做的:joy:

1.0版本我新建了一个脚本卡主了,直接就把我吓退了。

把你的实现方法也分享一下啊:clap:

mark11111

mark…

:grinning:

研究一下。mark

mark…学习了

const outRay = new geometry.ray();
ray已弃用 改成了Ray
另外为什么没有y轴啊?

标题写了地面,所以y是0

谢谢,理解了,因为是3D空间,Y轴(约等于高度)就是0
我做的小demo,是2D空间(3D相机)老搞混

mark!!!

let outRay: geometry.Ray = geometry.Ray.create();
let plane = geometry.Plane.create(0, 1, 0, 0);

this.mainCamera.screenPointToRay(screenX, screenY, outRay);
let distance = geometry.intersect.rayPlane(outRay, plane);

if(distance > 0){
outRay.computeHit(temp_v3, distance);
log(distance + " " +temp_v3);
}