当前位置:网站首页>游戏数学: 计算屏幕点中的平面上的点(上帝视角)
游戏数学: 计算屏幕点中的平面上的点(上帝视角)
2022-06-11 04:05:00 【番茄猿】
游戏数学: 计算屏幕上点中的平面上的点(上帝视角)

应用场景
上帝视角,点击屏幕,需要计算此时点中的平面上的位置,比如将物体移到点击的位置上,或者需要拖拽物体移动
数学计算
在这里,实际上是有几个前提的计算,或者说是简化版的射线与平面的交点
因为是上帝视角,所以射线的起点的y轴坐标一定为正,与y轴的夹角一定为锐角,且我们使用xy轴组成的平面作为交点计算的平面

我们通过Unity的屏幕点转化为射线,可以得到射线的起点和方向单位向量,我们假设射线起点为A点,与xy轴平面相交点为B点,A点在xy轴平面的投影点为C点,则根据向量计算,有 B点坐标 = A点坐标 + AB向量,所以我们只需求出AB的长度即可
故有求解公式如下:
AC长度 = ay
AB长度 = ay / cos(射线方向 与 AC向量的夹角)
B点 = A点 + 射线方向单位向量 * AB长度
在Unity中的示例
我们在Unity中完成这一计算的验证
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InteractiveTest : UIBehaviour, IPointerClickHandler
{
public Camera cam;
public GameObject ctrlObj;
public void OnPointerClick(PointerEventData pointerEventData)
{
var ray = cam.ScreenPointToRay(pointerEventData.position);
SetObjPos(ray.origin, ray.direction);
}
void SetObjPos(Vector3 origin, Vector3 direction)
{
float lengthAC = origin.y; //因为我们的相机的位置一定在xy平面上方
float cosABAC = -direction.y; //direction和(0, -1, 0)之间的夹角,即为AB和AC之间的夹角,一定是锐角
float lengthAB = lengthAC / cosABAC;
// Vector3 AB = lengthAB * direction;
// Vector3 CB = new Vector3(AB.x, 0, AB.z);
// Vector3 C = new Vector3(origin.x, 0, origin.z);
// Vector3 B = new Vector3(C.x + CB.x, 0, C.z + CB.z);
// ctrlObj.transform.position = B;
Vector3 B = origin + direction * lengthAB;
ctrlObj.transform.position = B;
}
}
我们简单的使用一个平面,一个球来做示例
点击屏幕任意位置,我们可以看到球都能准确的移到点击的位置上

总结
虽然该计算需要有条件限制,但是对于这种射线和平面相交的应用场景,我们使用这种计算方式消耗会更小,当然Unity的屏幕点击点转换到世界的射线也是可以拿出来自己计算的,这样子消耗就可以更小了。
边栏推荐
- 【服务器数据恢复】同友存储raid5崩溃的数据恢复案例
- Red team shooting range with three-layer protection
- Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
- 众昂矿业:氟化工是萤石的主要消耗领域
- Notes on redisson distributed lock usage
- [cnn]|differences between CNN and transformer
- App live broadcast source code, platform login page and password modification page
- Sslstrip Ultimate - location hijacking
- Embedded basic interface -spi
- 邪恶的CSRF
猜你喜欢

编程大作战 -- 挑战高考题

Docker uses PXC to build a MySQL Cluster (mysql:5.7.24)

Sslstrip Ultimate - location hijacking

Fundamentals of embedded audio processing

Cloud broadcast alert, guanghetong helps intelligent camera to build a "river protection" drowning prevention system

June 10, 2022: Captain Shu crosses a sweet potato field from north to South (m long from north to South and N wide from east to West). The sweet potato field is divided into 1x1 squares. He can start

超简单 CameraX 人脸识别效果封装

Guanghetong LTE Cat4 module l716 is upgraded to provide affordable and universal wireless applications for the IOT industry

ESP series module burning firmware

Matlab reports an error when trying to use * * * as a function problem, and tries to execute script PCA as a function:
随机推荐
写给通信年轻人的27个忠告
[激光器原理与应用-2]:国内激光器重点品牌
检测php网站是否已经被攻破的方法
给你一个项目,你将如何开展性能测试工作?
Summary of C language implementation of BP neural network
Why does the hospital comprehensive security system synchronize the NTP clock of the network server?
Eth Transfer
[network] socket programming
It's 2022. When will the "module freedom" be realized?
Manual testing cannot be changed to automated testing. What is missing?
Market prospect analysis and Research Report of denitrification unit in 2022
Cloud broadcast alert, guanghetong helps intelligent camera to build a "river protection" drowning prevention system
[cnn]| translation invariance
FreeRTOS startup - based on stm32
Eth Of Erc20 And Erc721
GPS Beidou time service, NTP makes network clock synchronization more accurate
2022 年 5 月产品大事记
Docker uses PXC to build a MySQL Cluster (mysql:5.7.24)
Matlab reports an error when trying to use * * * as a function problem, and tries to execute script PCA as a function:
Matter protocol