Invite d'architecture logique de physique de jeu
De Wikiprompt, l’encyclopédie libre de prompts
Invite d'architecture logique de physique de jeu Invite de système pour générer des implémentations techniques de physique de jeu, incluant des formules, des diagrammes d'état et du code.
Contenu du PromptEnregistrer
🌐
[Game Physics Logic Architect Mode]
**1. Mathematical Formulas (LaTeX)**
Let `P` be the player position, `A` the anchor point, `L0` the rope's rest length, `k` the spring constant, `b` the damping coefficient, and `v` the player's velocity.
Rope vector: `r = P - A`
Current length: `L = |r|`
Unit direction: `r̂ = r / L`
Elastic tension force (Hooke's Law with damping):
`F_spring = -k * (L - L0) * r̂ - b * (v · r̂) * r̂`
Total force on player: `F_total = F_spring + F_gravity + F_input`
Acceleration: `a = F_total / m`
Velocity update (semi-implicit Euler): `v_new = v + a * dt`
Position update: `P_new = P + v_new * dt`
For rope length constraint (if `L > L_max`), project player back:
`P_corrected = A + r̂ * L_max`
Momentum conservation: `m * v_before = m * v_after` (if no external impulse). For elastic collision with anchor, reflect velocity component along `r̂`: `v_reflected = v - 2 * (v · r̂) * r̂` (only if `v · r̂ > 0`).
Input-to-response latency: use fixed timestep `dt = 1/60` and buffer input for 1 frame (`input_buffer = 0.016s`).
---
**State Machine Transition Diagram (Markdown)**
```mermaid
stateDiagram-v2
[*] --> Idle
Idle --> Aiming: PressAndHold (Input)
Aiming --> Grappling: Release (Input) && RaycastHit
Aiming --> Idle: Release (Input) && NoHit
Grappling --> Swinging: RopeAttached
Swinging --> Reeling: PressReel (Input)
Swinging --> Detached: PressJump (Input) || RopeBreak
Reeling --> Swinging: ReleaseReel (Input)
Reeling --> Detached: RopeLength < Min
Detached --> Idle: PlayerGrounded
Swinging --> Idle: PlayerGrounded && RopeDetached
```
---
Production-Ready C# Code (Unity):
```csharp
using UnityEngine;
public class GrappleHook : MonoBehaviour
{
[Header("Physics")]
public float springConstant = 100f;
public float damping = 5f;
public float maxRopeLength = 20f;
public float minRopeLength = 2f;
public float playerMass = 1f;
[Header("Input")]
public KeyCode grappleKey = KeyCode.Mouse0;
public KeyCode reelKey = KeyCode.Mouse1;
public KeyCode jumpKey = KeyCode.Space;
private Vector3 anchorPoint;
private Rigidbody rb;
private bool isGrappling = false;
private bool isReeling = false;
private float currentRopeLength;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKeyDown(grappleKey))
{
TryAttachRope();
}
if (Input.GetKeyUp(grappleKey))
{
DetachRope();
}
if (Input.GetKeyDown(reelKey))
{
isReeling = true;
}
if (Input.GetKeyUp(reelKey))
{
isReeling = false;
}
if (Input.GetKeyDown(jumpKey))
{
DetachRope();
}
}
void FixedUpdate()
{
if (isGrappling)
{
ApplyRopePhysics();
}
}
void TryAttachRope()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity))
{
anchorPoint = hit.point;
currentRopeLength = Vector3.Distance(transform.position, anchorPoint);
isGrappling = true;
}
}
void ApplyRopePhysics()
{
Vector3 r = transform.position - anchorPoint;
float L = r.magnitude;
Vector3 rHat = r / L;
// Elastic tension force
Vector3 springForce = -springConstant * (L - currentRopeLength) * rHat;
Vector3 dampingForce = -damping * Vector3.Dot(rb.linearVelocity, rHat) * rHat;
Vector3 totalForce = springForce + dampingForce + Physics.gravity * rb.mass;
rb.AddForce(totalForce, ForceMode.Force);
// Rope length limit
if (L > currentRopeLength)
{
rb.linearVelocity -= Vector3.Dot(rb.linearVelocity, rHat) * rHat;
transform.position = anchorPoint + rHat * currentRopeLength;
}
// Reeling logic
if (isReeling)
{
currentRopeLength = Mathf.Max(minRopeLength, currentRopeLength - 2f * Time.fixedDeltaTime);
}
}
void DetachRope()
{
isGrappling = false;
isReeling = false;
}
}
```
Note: The code uses `rb.linearVelocity` (Unity 6+). For older versions, use `rb.velocity`. The `ForceMode.5` is a placeholder - replace with `ForceMode.Force` or `ForceMode.Acceleration` as needed.
Connectez-vous pour voir le prompt complet
Continuer avec:
En vous connectant, vous acceptez nos Conditions et Confidentialité
Utilisation
Ce prompt est conçu pour être utilisé avec coding. Copiez le contenu ci-dessus et collez-le dans votre outil d’IA préféré.
Pour de meilleurs résultats, personnalisez les espaces réservés (indiqués par des crochets ou des majuscules) selon vos besoins.
Discussion
0 commentaires