讨论

游戏机制工程师:太空漂移控制器

来自 Wikiprompt,自由的提示词百科全书

loshu2000

2026年4月29日

游戏机制工程师:太空漂移控制器 用于生成基于物理学的游戏机制代码的系统提示,专注于牛顿运动与弹丸拦截的C#脚本。

提示词内容收藏

🌐
我将作为游戏机制工程师,提供核心运动与投射物逻辑。输出将包含数学推导和性能优化脚本(C#),仅聚焦牛顿物理、矢量速度叠加与高频碰撞轮询。 --- **数学推导:投射物拦截(相对速度矢量叠加)** 设: - 舰船当前速度矢量:**V_ship** = (Vx, Vy) - 武器基础出膛速度(相对舰船):**V_muzzle** = (Vm_x, Vm_y)(通常沿舰船朝向,但可任意) - 投射物绝对速度:**V_proj** = **V_ship** + **V_muzzle** = (Vx + Vm_x, Vy + Vm_y) 拦截目标(假设目标速度恒定 **V_target**,位置 **P_target**,投射物发射位置 **P_proj**): - 相对位置:**D** = **P_target** - **P_proj** - 相对速度:**V_rel** = **V_target** - **V_proj** - 求解时间 t(标量)满足:|**D** + **V_rel** * t| = 0(即碰撞点) 展开为二次方程: - |**V_rel**|² * t² + 2 * (**D** · **V_rel**) * t + |**D**|² = 0 - 判别式:Δ = (2 * **D** · **V_rel**)² - 4 * |**V_rel**|² * |**D**|² - 若 Δ ≥ 0,则 t = (-2 * (**D** · **V_rel**) - √Δ) / (2 * |**V_rel**|²)(取最小正根) - 拦截点:**P_impact** = **P_target** + **V_target** * t --- **性能优化脚本(C#,Unity风格,但纯逻辑可移植)** ```csharp using UnityEngine; public class DriftShipController : MonoBehaviour { [Header("Ship Physics")] public float maxThrust = 20f; public float linearDrag = 0.95f; // 每帧速度衰减系数(0.9-0.99) public float angularSpeed = 180f; // 度/秒 private Vector2 velocity; private float heading; // 弧度 void Update() { // 输入(假设:W/S 推进,A/D 转向) float thrustInput = Input.GetAxis("Vertical"); float turnInput = -Input.GetAxis("Horizontal"); // 转向(角速度) heading += turnInput * angularSpeed * Mathf.Deg2Rad * Time.deltaTime; // 推进力(沿舰船朝向) Vector2 thrustDir = new Vector2(Mathf.Cos(heading), Mathf.Sin(heading)); velocity += thrustDir * (maxThrust * thrustInput * Time.deltaTime); // 惯性阻尼(线性拖拽) velocity *= Mathf.Pow(linearDrag, Time.deltaTime * 60f); // 帧率无关 // 位置更新(牛顿积分) transform.position += (Vector3)(velocity * Time.deltaTime); } // 武器发射:返回投射物绝对速度 public Vector2 FireProjectile(Vector2 muzzleVelocityLocal) { // 旋转局部出膛速度到世界坐标 Vector2 muzzleWorld = Rotate(muzzleVelocityLocal, heading); return velocity + muzzleWorld; // 关键:相对速度叠加 } private Vector2 Rotate(Vector2 v, float angle) { float cos = Mathf.Cos(angle); float sin = Mathf.Sin(angle); return new Vector2(v.x * cos - v.y * sin, v.x * sin + v.y * cos); } } // 投射物管理器(高频碰撞轮询) public class ProjectilePool : MonoBehaviour { public GameObject projectilePrefab; public int poolSize = 100; public float collisionRadius = 0.1f; public LayerMask targetMask; private Projectile[] pool; private int activeCount = 0; void Start() { pool = new Projectile[poolSize]; for (int i = 0; i < poolSize; i++) { pool[i] = Instantiate(projectilePrefab).GetComponent<Projectile>(); pool[i].gameObject.SetActive(false); } } public void Spawn(Vector2 position, Vector2 velocity, float lifetime) { if (activeCount >= poolSize) return; Projectile p = pool[activeCount++]; p.Init(position, velocity, lifetime, collisionRadius, targetMask); } void FixedUpdate() // 固定频率轮询(推荐50Hz或更高) { for (int i = 0; i < activeCount; i++) { if (!pool[i].Step(Time.fixedDeltaTime)) { // 移除失效投射物(交换到末尾) pool[i] = pool[--activeCount]; pool[activeCount].gameObject.SetActive(false); i--; } } } } public class Projectile : MonoBehaviour { private Vector2 position; private Vector2 velocity; private float lifeRemaining; private float radius; private LayerMask mask; public void Init(Vector2 pos, Vector2 vel, float life, float r, LayerMask m) { position = pos; velocity = vel; lifeRemaining = life; radius = r; mask = m; gameObject.SetActive(true); transform.position = pos; } public bool Step(float dt) { lifeRemaining -= dt; if (lifeRemaining <= 0f) return false; // 子步进碰撞检测(高频轮询,避免隧穿) int substeps = 4; // 每帧4次子步,等效4倍频率 float subDt = dt / substeps; for (int i = 0; i < substeps; i++) { position += velocity * subDt; transform.position = position; // 圆形碰撞检测(Physics2D.OverlapCircle 或自定义空间哈希) Collider2D hit = Physics2D.OverlapCircle(position, radius, mask); if (hit != null) { // 命中处理(此处仅销毁) return false; } } return true; } } ``` --- **性能优化要点**: 1. **对象池**:避免GC分配,固定内存。 2. **子步进**:将固定时间步分割为4个子步,等效碰撞轮询频率提升至200Hz(若FixedUpdate为50Hz),有效防止高速投射物隧穿。 3. **矢量叠加**:投射物速度在发射瞬间计算一次,后续无额外开销。 4. **拖拽公式**:使用 `Mathf.Pow` 实现帧率无关的指数衰减,避免帧率波动影响物理一致性。 --- **拦截推导示例(数值验证)**: - 舰船速度 (10, 0),出膛速度 (0, 50) → 投射物绝对速度 (10, 50) - 目标位置 (100, 100),目标速度 (-5, 0) - 相对速度 = (-5-10, 0-50) = (-15, -50) - 相对位置 = (100-0, 100-0) = (100, 100) - 方程:(-15t+100)² + (-50t+100)² = 0 → 展开得 2725t² - 13000t + 20000 = 0 - Δ = 169000000 - 4*2725*20000 = 169000000 - 218000000 = -49000000 < 0 → 无解(无法拦截,需调整发射角度) 若调整出膛速度方向(如 (30, 40)),则绝对速度 (40, 40),相对速度 (-45, -40),相对位置 (100, 100): - 方程:(-45t+100)² + (-40t+100)² = 0 → 3625t² - 17000t + 20000 = 0 - Δ = 289000000 - 4*3625*20000 = 289000000 - 290000000 = -1000000 < 0 → 仍无解(目标远离) 实际拦截需满足目标在投射物速度锥内,上述推导用于AI瞄准或预判系统。 --- 输出完毕。仅包含运动逻辑、数学推导与脚本,无故事/UI/NPC内容。

登录以查看完整提示词

继续使用:

登录即表示你同意我们的 使用条款 和 隐私政策

用法

此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。

为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。

参考资料

分类:coding| prompts.chat| game-mechanics| physics

讨论