当前位置:网站首页>Game Mathematics: calculate the points on the plane in the screen points (God's perspective)
Game Mathematics: calculate the points on the plane in the screen points (God's perspective)
2022-06-11 04:17:00 【Tomato ape】
Game math : Calculate the points on the plane in the points on the screen ( God's perspective )

Application scenarios
God's perspective , Click on the screen , You need to calculate the position on the plane at this point , For example, move the object to the clicked position , Or you need to drag objects to move
Mathematical calculation
ad locum , It's actually a calculation with several premises , Or the intersection of a simplified version of a ray and a plane
Because it's God's perspective , So the starting point of the ray is y The axis coordinates must be positive , And y The included angle of the axis must be an acute angle , And we use xy The plane composed of axes is used as the plane for intersection calculation

We go through Unity The screen points of are converted into rays , You can get the starting point and direction unit vector of the ray , We assume that the starting point of the ray is A spot , And xy The intersection point of the axis plane is B spot ,A The point is xy The projection point of the axis plane is C spot , Then calculate according to the vector , Yes B Point coordinates = A Point coordinates + AB vector , So we just need to AB It's just as long as you want
Therefore, the solution formula is as follows :
AC length = ay
AB length = ay / cos( The direction of the ray And AC The angle of the vector )
B spot = A spot + Ray direction unit vector * AB length
stay Unity Example in
We are Unity To complete the verification of this calculation
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; // Because the location of our camera must be xy Above the plane
float cosABAC = -direction.y; //direction and (0, -1, 0) The Angle between , That is to say AB and AC The Angle between , It must be an acute angle
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;
}
}
We simply use a plane , A ball as an example 
Click anywhere on the screen , We can see that the ball can move to the clicked position accurately 

summary
Although the calculation needs to be conditional , But for this kind of application scenario where rays and planes intersect , We use this calculation method to consume less , Of course Unity The screen click points converted to the rays of the world can also be calculated by themselves , In this way, the sub consumption can be reduced .
边栏推荐
- 密码找回功能可能存在的问题(补充)
- 未來已來,5G-Advanced時代開啟
- Several time synchronization methods of Beidou timing system (GPS timing equipment)
- 2022 年 5 月产品大事记
- Talk about data center network again
- [激光器原理与应用-2]:国内激光器重点品牌
- Market prospect analysis and Research Report of beam combiner in 2022
- 基于FPGA的一维卷积神经网络CNN的实现(五)数据量化(附代码)
- Vulkan official example interpretation shadows (rasterization)
- 给你一个项目,你将如何开展性能测试工作?
猜你喜欢

2022爱分析· 隐私计算厂商全景报告 | 爱分析报告

数据分析师必知必会的统计学知识

Esp32 development -lvgl display picture

Discussion on the development trend of remote power management unit (Intelligent PDU)

Statistical knowledge required by data analysts

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

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

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

Sslstrip Ultimate - location hijacking

JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器
随机推荐
It's 2022. When will the "module freedom" be realized?
Talk about data center network again
App live broadcast source code, platform login page and password modification page
Esp32 development -lvgl display picture
Esp32 development -lvgl animation display
L'avenir est venu, l'ère 5G - Advanced s'ouvre
Market prospect analysis and Research Report of programmable digital temperature regulator in 2022
What is the time-consuming domain name resolution? What are the influencing factors of domain name resolution time?
Exploitation and utilization of clickjacking vulnerability
司马炎爷爷 告诉你什么叫做内卷!
[激光器原理与应用-2]:国内激光器重点品牌
Implementation of one-dimensional convolutional neural network CNN based on FPGA (V) data quantization (with code)
检测php网站是否已经被攻破的方法
Evil CSRF
再聊数据中心网络
Matter protocol
golang泛型:generics
Feature selection algorithm based on bare bones particleswarm optimization
Market prospect analysis and Research Report of denitrification unit in 2022
2022 love analysis · privacy computing vendor panoramic report | love Analysis Report