Last active
March 27, 2025 17:04
-
-
Save w0wca7a/5b3524d4c7239c3762b8b6bc11b31252 to your computer and use it in GitHub Desktop.
Движение объекта по эллипсу и вращение относительно оси движения. Motion of an object along an ellipse and rotation about the axis of motion. Script for Stride Game Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) Stride contributors (https://stride3d.net). | |
| // Distributed under the MIT license. | |
| using System; | |
| using Stride.Core.Mathematics; | |
| using Stride.Engine; | |
| [Display("Elliptical Movement with Entity Rotation")] | |
| [ComponentCategory("Movements")] | |
| [DataContract(nameof(EllipticalMovementRotateEntity))] | |
| public class EllipticalMovementRotateEntity : SyncScript | |
| { | |
| // Большая полуось (по оси X) | |
| [DataMember] | |
| [Display(1, "Major semi-axis")] | |
| public float SemiMajorAxis = 5.0f; | |
| // Малая полуось (по оси Z) | |
| [DataMember] | |
| [Display(2, "Minor semi-axis")] | |
| public float SemiMinorAxis = 3.0f; | |
| // Скорость движения (радианы в секунду) | |
| [DataMember] | |
| [Display(3, "Speed of movement")] | |
| public float Speed = 1.0f; | |
| // Текущий угол | |
| private float angle = 0.0f; | |
| public override void Update() | |
| { | |
| // Увеличиваем угол в зависимости от времени и скорости | |
| angle += Speed * (float)Game.UpdateTime.Elapsed.TotalSeconds; | |
| // Вычисляем новые координаты по эллипсу | |
| float x = SemiMajorAxis * MathF.Cos(angle); | |
| float z = SemiMinorAxis * MathF.Sin(angle); | |
| // Получаем текущую позицию сущности | |
| var position = Entity.Transform.Position; | |
| // Обновляем позицию сущности | |
| position.X = x; | |
| position.Z = z; | |
| Entity.Transform.Position = position; | |
| // Вычисляем направление движения (касательный вектор) | |
| float tangentX = -SemiMajorAxis * MathF.Sin(angle); // Производная по X | |
| float tangentZ = SemiMinorAxis * MathF.Cos(angle); // Производная по Z | |
| // Вычисляем угол поворота сущности | |
| float rotationAngle = MathF.Atan2(tangentZ, tangentX); | |
| // Применяем поворот сущности | |
| Entity.Transform.Rotation = Quaternion.RotationY(-rotationAngle); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment