Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
MovementController.Integrated.cs
1using System.Drawing;
2using System.Numerics;
3
5
9public sealed partial class MovementController
10{
16 public void SetVelocity(Vector2 velocity)
17 {
19 _state.Velocity = velocity;
20 }
21
27 public void SetAcceleration(Vector2 acceleration)
28 {
30 _state.Acceleration = acceleration;
31 }
32
38 public void SetMaxSpeed(float? maxSpeed)
39 {
40 _state.MaxSpeed = maxSpeed;
41 }
42
49 public void SetLinearDamping(float dampingPerSec)
50 {
51 _state.LinearDamping = MathF.Max(0f, dampingPerSec);
52 }
53
54 private bool AdvanceIntegrated(float dt)
55 {
56 if (_state.HasMotion)
57 {
58 Step(dt); // physics (accel/vel/damping/wrap/space-convert)
59 return true;
60 }
61
62 return false;
63 }
64
69 private void Step(float dt)
70 {
71 // integrate kinematics in the mover's own space
72 _state.Velocity += _state.Acceleration * dt;
73 ClampVelocity();
74
75 // exponential damping (frame-rate independent)
76 if (_state.LinearDamping > 0f)
77 _state.Velocity *= MathF.Exp(-_state.LinearDamping * dt);
78
79 // kill tiny velocities when no accel
80 const float StopEpsilon = 0.01f;
81 if (_state.Acceleration == Vector2.Zero &&
82 _state.Velocity.LengthSquared() < StopEpsilon * StopEpsilon)
83 _state.Velocity = Vector2.Zero;
84
85 // move in the mover's space
86 var pos = _mover.GetPosition();
87 var newPos = pos + _state.Velocity * dt;
88
89 // optional wrapping if mover lives in Grid space
90 if (_mover.PositionSpace == MovementSpace.Grid && (WrapX || WrapY))
91 {
92 if (_coords is null || _sceneLayer is null)
93 throw new InvalidOperationException("Grid wrapping requires coordinates/layer.");
94
95 var wrapped = _coords.FindEquivalentSceneLayerCoordinates(
96 new PointF(newPos.X, newPos.Y),
97 _sceneLayer.GridColumnCount - 1, _sceneLayer.GridRowCount - 1);
98
99 float newX = newPos.X;
100 float newY = newPos.Y;
101
102 if (WrapX)
103 newX = wrapped.X;
104
105 if (WrapY)
106 newY = wrapped.Y;
107
108 newPos = new Vector2(newX, newY);
109 }
110
111 _mover.SetPosition(newPos);
112 }
113
122 internal void ZeroVelocityComponent(bool zeroX, bool zeroY)
123 {
124 if (!zeroX && !zeroY)
125 return;
126
127 var v = _state.Velocity;
128 _state.Velocity = new Vector2(zeroX ? 0f : v.X, zeroY ? 0f : v.Y);
129 }
130
137 private void ClampVelocity()
138 {
139 if (_state.MaxSpeed is null)
140 return;
141
142 var v = _state.Velocity;
143 var speed = v.Length();
144 var max = _state.MaxSpeed.Value;
145
146 if (max > 0 && speed > max)
147 _state.Velocity = v * (max / speed);
148 }
149}
Central controller responsible for managing all movement modes for a single IMovable object....
bool WrapX
Enables or disables horizontal world wrapping. When enabled, movement crossing the left/right edges w...
bool WrapY
Enables or disables vertical world wrapping. When enabled, movement crossing the top/bottom edges wra...
void SetMaxSpeed(float? maxSpeed)
Sets the maximum speed for the current state. Only applies to integrated movement.
void CancelScript()
Cancels any active scripted movement and clears the current script state. Raises ScriptedMovementStop...
void SetLinearDamping(float dampingPerSec)
Sets the linear damping value, which determines the rate at which linear motion is reduced over time.
void SetAcceleration(Vector2 acceleration)
Sets the acceleration of the object.
void SetVelocity(Vector2 velocity)
Sets the velocity of the object.
Vector2 GetPosition()
Get the current position in the mover's PositionSpace.
void SetPosition(Vector2 pos)
Set the position in the mover's PositionSpace.
MovementSpace PositionSpace
Which unit system this mover uses for its position.
Definition IMovable.cs:14
MovementSpace
Specifies the coordinate system used for movement calculations and updates.
Vector2 Velocity
Velocity in current MotionSpace units per second.
Vector2 Acceleration
Acceleration in current MotionSpace units per second^2.
float LinearDamping
Linear damping per second in [0..1]. 0 = no damping. Apply as v *= (1 - LinearDamping * dt) in the co...
readonly bool HasMotion
Gets a value indicating whether the object is in motion.