当前位置:网站首页>游戏数学: 计算屏幕点中的平面上的点(上帝视角)
游戏数学: 计算屏幕点中的平面上的点(上帝视角)
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的屏幕点击点转换到世界的射线也是可以拿出来自己计算的,这样子消耗就可以更小了。
边栏推荐
- A - Eddy's AC puzzle (C language)
- 三层带防护内网红队靶场
- Market prospect analysis and Research Report of modular lithium ion battery in 2022
- Esp32 gattc configuration UUID
- Google 有哪些牛逼的开源项目?
- Embedded basic interface-i2c
- 店铺门面转让出租小程序开发制作功能介绍
- 众昂矿业:氟化工是萤石的主要消耗领域
- Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
- Matlab reports an error when trying to use * * * as a function problem, and tries to execute script PCA as a function:
猜你喜欢

Programming battle -- challenging college entrance examination questions

AI助力,释放法务势能!iTerms合同智审系统重磅发布

写给通信年轻人的27个忠告

Docker swarm installing MySQL Cluster

Eth Transfer

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

This artifact is highly recommended. One line command will convert the web page to PDF!

6. 表格标签

A Security Analysis Of Browser Extensions

为了实现零丢包,数据中心网络到底有多拼?
随机推荐
如何检查域名解析是否生效?
7. list label
FreeRTOS startup - based on stm32
Student teacher examination management system based on SSM framework
How to invest in programming knowledge and reduce the impact of knowledge failure
Market prospect analysis and Research Report of surround packing machine in 2022
【服务器数据恢复】同友存储raid5崩溃的数据恢复案例
软件工程笔记之期末复习(简答)
数据分析师必知必会的统计学知识
Esp32 development -lvgl display picture
Notes on redisson distributed lock usage
Market prospect analysis and Research Report of engraving laser in 2022
【网络篇】套接字编程
Samsung Galaxy S21 ultra and Apple iPhone 13 Pro Max: which one should you choose
基于FPGA的一维卷积神经网络CNN的实现(五)数据量化(附代码)
使用工具类按一定规则读取Excel文件
How PTP helps several major operators meet 5g timing requirements
三层带防护内网红队靶场
Market prospect analysis and Research Report of Ethernet scanner in 2022
Detailed explanation of network time synchronization (NTP network timing) of video monitoring system