当前位置:网站首页>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 .
边栏推荐
- 2022-06-10:薯队长从北向南穿过一片红薯地(南北长M,东西宽N),红薯地被划分为1x1的方格, 他可以从北边的任何一个格子出发,到达南边的任何一个格子, 但每一步只能走到东南、正南、西南方向的
- Zhongang Mining: fluorochemical industry is the main consumption field of fluorite
- AI helps release legal potential energy! Release of iterms contract intelligent review system
- Market prospect analysis and Research Report of beam combiner in 2022
- Market prospect analysis and Research Report of welding laser in 2022
- Market prospect analysis and Research Report of pipe and hose press fitting tools in 2022
- 使用工具类按一定规则读取Excel文件
- Esp32 development -lvgl display picture
- A - Eddy's AC puzzle (C language)
- Lexical analyzer for compiling principle notes
猜你喜欢

从初代播种到落地生花,5G商用三周年“催生万物”

JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器

Introduction to the development and production functions of shop facade transfer and rental applet

Fundamentals of embedded audio processing

JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术

Guanghetong won the "science and Technology Collaboration Award" of Hello travel, driving two rounds of green industries to embrace digital intelligence transformation

A Security Analysis Of Browser Extensions

ESP series module burning firmware

NTP time server (GPS Beidou satellite synchronous clock) application boiler monitoring system

JVM (2): loading process of memory structure and classes
随机推荐
Embedded basic interface PWM
7. list label
写给通信年轻人的27个忠告
7. 列表标签
clickjacking漏洞的挖掘与利用
JVM(4):类的主动使用与被动使用、运行时数据区域内部结构、JVM线程说明、PC寄存器
Given a project, how will you conduct performance testing?
Guanghetong won the "science and Technology Collaboration Award" of Hello travel, driving two rounds of green industries to embrace digital intelligence transformation
Esp32 porting lvgl
Safe and borderless, Guanghe tongdai 5g module +ai intelligent security solution shines at CPSE Expo
Programming battle -- challenging college entrance examination questions
Market prospect analysis and Research Report of programmable digital temperature regulator in 2022
Summary of C language implementation of BP neural network
游戏数学: 计算屏幕点中的平面上的点(上帝视角)
关于重复发包的防护与绕过
编译原理笔记之词法分析器
ESP8266_ RTOS modifies IP address and hostname in AP mode
[laser principle and application-2]: key domestic laser brands
NTP time server (GPS Beidou satellite synchronous clock) application boiler monitoring system
Some differences between people