游戏开发中的数学问题:技能伤害的区域检测(扇形)
2023-06-13 09:17:08 时间
扇形范围判断,其实我们可以理解为一个圆形的判断变种,只是在特定的圆形角度里面才会判断,如果不在这个角度中,直接返回一个false即可.
首先还是先画出一个扇形,我们这样来设计,比如这个扇形的角度是60度,我们分为左右,一边30度,这样对于程序好写一点,首先画出扇形的两个边:
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Fanshaped : MonoBehaviour
6{
7 public float angel = 60;
8 public float radius = 4;
9
10
11 private void OnDrawGizmos()
12 {
13 //
14 float x = radius * Mathf.Sin(angel / 2 * Mathf.Deg2Rad);//角度转换为弧度
15 float y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
16 Vector3 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
17 Vector3 b = new Vector3(transform.position.x + x, 0, transform.position.z + y);
18
19 Gizmos.DrawLine(transform.position, a);
20 Gizmos.DrawLine(transform.position, b);
21
22
23
24 }
25}
然后就是和圆形差不多的算法,绘制一个弧形:
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Fanshaped : MonoBehaviour
6{
7 public float angel = 60;
8 public float radius = 4;
9 public int count = 10;//扇形的精度
10
11 private void OnDrawGizmos()
12 {
13 //
14 float x = radius * Mathf.Sin(angel / 2 * Mathf.Deg2Rad);//角度转换为弧度
15 float y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
16 Vector3 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
17 Vector3 b = new Vector3(transform.position.x + x, 0, transform.position.z + y);
18
19 Gizmos.DrawLine(transform.position, a);
20 Gizmos.DrawLine(transform.position, b);
21
22 float half = angel / 2;
23
24 for (int i = 0; i <= count ; i++)
25 {
26
27 float temp = (half / count);
28 temp *= i;
29 x = radius * Mathf.Sin((temp) * Mathf.Deg2Rad);
30 y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
31 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
32 Gizmos.DrawSphere(a, 0.05f);
33 x = radius * Mathf.Sin((-temp) * Mathf.Deg2Rad);
34 y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
35 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
36 Gizmos.DrawSphere(a, 0.05f);
37 }
38
39 }
40}
绘制的弧形如下:
下面我们开始判断点是否在扇形内:
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class Fanshaped : MonoBehaviour
6{
7 public Transform Point;
8
9 public float angel = 60;
10 public float radius = 4;
11 public int count = 10;//扇形的精度
12
13 private void OnDrawGizmos()
14 {
15 //
16 float x = radius * Mathf.Sin(angel / 2 * Mathf.Deg2Rad);//角度转换为弧度
17 float y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
18 Vector3 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
19 Vector3 b = new Vector3(transform.position.x + x, 0, transform.position.z + y);
20
21 Gizmos.DrawLine(transform.position, a);
22 Gizmos.DrawLine(transform.position, b);
23
24 float half = angel / 2;
25
26 if (IsInRange(Point))
27 {
28 Gizmos.color = Color.red;
29 }
30 else
31 {
32 Gizmos.color = Color.blue;
33 }
34
35 for (int i = 0; i <= count ; i++)
36 {
37
38 float temp = (half / count);
39 temp *= i;
40 x = radius * Mathf.Sin((temp) * Mathf.Deg2Rad);
41 y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
42 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
43 Gizmos.DrawSphere(a, 0.05f);
44 x = radius * Mathf.Sin((-temp) * Mathf.Deg2Rad);
45 y = Mathf.Sqrt(Mathf.Pow(radius, 2) - Mathf.Pow(x, 2f));
46 a = new Vector3(transform.position.x - x, 0, transform.position.z + y);
47 Gizmos.DrawSphere(a, 0.05f);
48 }
49
50 }
51
52
53 public bool IsInRange( Transform target)
54 {
55 //攻击者位置指向目标位置的向量
56 Vector3 direction = target.position - transform.position;
57 //点乘积结果
58 float dot = Vector3.Dot(direction.normalized, transform.forward);
59 //反余弦计算角度
60 float offsetAngle = Mathf.Acos(dot) * Mathf.Rad2Deg;
61 return offsetAngle < angel * .5f && direction.magnitude < radius ;
62 }
63}
我们像之前一样的去测试一下.
如果点在扇形内:
如果不在:
引申
那么如果是扇形的环呢?其实,我们只需要判断点到圆心的距离,是否是在环内就可以了.
相关文章
- Humble Bundle联合EA游戏大促销
- CSO面对面|对话因陀罗,共话游戏行业安全防护之路
- 有趣的在线编程游戏,在玩游戏的同时掌握编程!
- 网页游戏怎么开发的_网页软件游戏开发
- laya游戏开发之贪吃蛇大作战(二)—— 贪吃蛇客户端
- Gamepad Tester游戏手柄在线测试平台
- 游戏和区块链
- NFT卡牌游戏系统开发Web3链游技术
- 2021年游戏产业舆情生态报告,网易跟投元宇宙UGC平台,
- 游戏开发中的数学问题:技能伤害的区域检测(方形,圆形,三角形,扇形)
- H5/小程序小游戏合成游戏系统开发解析方案
- 封神之作,良心的开源游戏开发平台
- 微信小游戏开发如何选游戏引擎?
- 互联网医疗的“鱿鱼游戏”
- 基于QT开发的开源局域网联机UNO卡牌游戏报告(附github仓库地址)
- 微信云开发被释放了,重新写了个足球游戏
- 【Android 应用开发】Android游戏音效实现
- 【错误记录】Visual Studio 2019 中运行 Unity C# 脚本时报错 ( 根据解决方案, 可能需要安装额外的组件才能获得 | .NET 桌面开发 | 使用 Unity 的游戏开发 )
- 快速开发游戏的十个关键
- [视频]DIY达人成功将PS2改造成游戏掌机
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 玩游戏分辨率翻车?仅仅10K的超实用游戏神器
- 使用 Linux 搭建游戏服务器:建立顺畅稳定的游戏体验(linux搭建游戏)
- 游戏行业大展宏图Redis面试分析(游戏公司redis面试)
- 游戏开发实现高效的Redis数据库存储(游戏redis数据库)
- 简单快捷下载游戏手机必备MySQL数据库(MYSQL下载游戏手机)
- 如何下载MySQL用于游戏App(MYSQL下载游戏app)
- 网易首款VR游戏幕后有着怎样的开发故事 | 解密
- MR 内容怎么做?Magic Leap 想让你玩“活见鬼”的游戏
- php网页游戏开发入门教程一(webgame+design)
- node.js适合游戏后台开发吗?