add spline based motion

pull/1/head
cosmonaut 2020-08-26 14:58:01 -07:00
parent bf913716e7
commit eb51182760
11 changed files with 147 additions and 29 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "Smuggler"]
path = Smuggler
url = https://gitea.moonside.games/MoonsideGames/Smuggler.git
[submodule "MoonTools.Curve"]
path = MoonTools.Curve
url = https://gitea.moonside.games/MoonsideGames/MoonTools.Curve.git

View File

@ -0,0 +1,31 @@
using Encompass;
using KavTest.Extensions;
using MoonTools.Curve;
namespace KavTest.Components
{
public struct MoveAlongCurve3DComponent : IComponent
{
public SplineCurve3D Curve { get; }
public float Time { get; }
public MoveAlongCurve3DComponent(SplineCurve3D curve)
{
Curve = curve;
Time = 0;
}
public MoveAlongCurve3DComponent(SplineCurve3D curve, float currentTime)
{
Curve = curve;
Time = currentTime;
}
public Microsoft.Xna.Framework.Vector3 Position
{
get
{
return Curve.Point(Time).ToXNAVector();
}
}
}
}

Binary file not shown.

View File

@ -0,0 +1,29 @@
using Encompass;
using KavTest.Components;
using KavTest.Messages;
namespace KavTest.Engines
{
[Sends(typeof(TranslationMessage))]
[Writes(typeof(MoveAlongCurve3DComponent))]
[QueryWith(typeof(MoveAlongCurve3DComponent), typeof(Transform3DComponent))]
public class MoveAlongCurve3DEngine : Engine
{
public override void Update(double dt)
{
foreach (var entity in TrackedEntities)
{
ref readonly var movealongCurveComponent = ref GetComponent<MoveAlongCurve3DComponent>(entity);
ref readonly var transformComponent = ref GetComponent<Transform3DComponent>(entity);
var newCurve = new MoveAlongCurve3DComponent(
movealongCurveComponent.Curve,
movealongCurveComponent.Time + (float)dt
);
SendMessage(new TranslationMessage(entity, newCurve.Position - transformComponent.Transform.Position));
SetComponent(entity, newCurve);
}
}
}
}

View File

@ -1,4 +1,5 @@
using Encompass;
using Kav;
using KavTest.Components;
using KavTest.Messages;
@ -6,12 +7,21 @@ namespace KavTest.Spawners
{
public class LightBulbSpawner : Spawner<LightBulbSpawnMessage>
{
public Model LightBulbModel { get; }
public LightBulbSpawner(Model lightBulbModel)
{
LightBulbModel = lightBulbModel;
}
protected override void Spawn(in LightBulbSpawnMessage message)
{
var entity = CreateEntity();
AddComponent(entity, new Transform3DComponent(message.Transform));
AddComponent(entity, new PointLightComponent(message.Color, message.Intensity));
AddComponent(entity, new MoveAlongCurve3DComponent(message.Curve));
AddComponent(entity, new ModelComponent(LightBulbModel));
}
}
}

View File

@ -0,0 +1,10 @@
namespace KavTest.Extensions
{
public static class Vector3Extensions
{
public static Microsoft.Xna.Framework.Vector3 ToXNAVector(this System.Numerics.Vector3 vector)
{
return new Microsoft.Xna.Framework.Vector3(vector.X, vector.Y, vector.Z);
}
}
}

View File

@ -27,6 +27,7 @@
<ProjectReference Include="..\encompass-cs\encompass-cs\encompass-cs.csproj" />
<ProjectReference Include="..\Kav\Kav.Core.csproj" />
<ProjectReference Include="..\Smuggler\Smuggler.Core.csproj" />
<ProjectReference Include="..\MoonTools.Curve\Curve\Curve.csproj" />
</ItemGroup>
<Import Project="..\build\CopyFNALibs.targets"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

View File

@ -30,6 +30,7 @@
<ProjectReference Include="..\encompass-cs\encompass-cs\encompass-cs.csproj" />
<ProjectReference Include="..\Kav\Kav.Framework.csproj" />
<ProjectReference Include="..\Smuggler\Smuggler.Framework.csproj" />
<ProjectReference Include="..\MoonTools.Curve\Curve\Curve.csproj" />
</ItemGroup>
<Import Project="..\build\CopyFNALibs.targets"/>
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

View File

@ -1,3 +1,4 @@
using System.Collections.Immutable;
using System.IO;
using Encompass;
using KavTest.Components;
@ -7,6 +8,7 @@ using KavTest.Renderers;
using KavTest.Spawners;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MoonTools.Curve;
namespace KavTest
{
@ -50,12 +52,18 @@ namespace KavTest
Smuggler.Importer.ImportGLB(GraphicsDevice, File.OpenRead("Content/rustysphere.glb"))
);
var lightBulbModel = Kav.ModelLoader.Load(
GraphicsDevice,
Smuggler.Importer.ImportGLB(GraphicsDevice, File.OpenRead("Content/cube.glb"))
);
WorldBuilder.AddEngine(new InputEngine(this));
WorldBuilder.AddEngine(new AngularVelocityEngine());
WorldBuilder.AddEngine(new MoveAlongCurve3DEngine());
WorldBuilder.AddEngine(new MotionEngine());
WorldBuilder.AddEngine(new CameraEngine());
WorldBuilder.AddEngine(new RustyBallSpawner(rustyBallModel));
WorldBuilder.AddEngine(new LightBulbSpawner());
WorldBuilder.AddEngine(new LightBulbSpawner(lightBulbModel));
WorldBuilder.AddGeneralRenderer(new SceneRenderer(GraphicsDevice), 0);
WorldBuilder.SendMessage(new RustyBallSpawnMessage(
@ -73,41 +81,62 @@ namespace KavTest
new Vector3(-1, 1, 1)
));
var lightMovementSpline = new SplineCurve3D(
ImmutableArray.Create<ICurve3D>(
new QuadraticBezierCurve3D(
new System.Numerics.Vector3(-5, 2, -5),
new System.Numerics.Vector3(20, 50, -20),
new System.Numerics.Vector3(10, 20, 10)
),
new QuadraticBezierCurve3D(
new System.Numerics.Vector3(10, 20, 10),
new System.Numerics.Vector3(-20, -50, -10),
new System.Numerics.Vector3(-5, 2, -5)
)
),
ImmutableArray.Create<float>(
3f,
3f
),
true
);
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-5, 2, -5)),
new Transform3D(new Vector3(-5, 2, -5), Quaternion.Identity, new Vector3(0.25f, 0.25f, 0.25f)),
Color.White,
300f
300f,
lightMovementSpline
));
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(5, 2, -5)),
Color.Blue,
300f
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(5, 2, -5)),
// Color.Blue,
// 300f
// ));
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-5, -2, -5)),
Color.Red,
300f
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, -2, -5)),
// Color.Red,
// 300f
// ));
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-5, 2, 5)),
Color.Yellow,
300f
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, 2, 5)),
// Color.Yellow,
// 300f
// ));
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-5, 2, -10)),
Color.Orange,
300f
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-5, 2, -10)),
// Color.Orange,
// 300f
// ));
WorldBuilder.SendMessage(new LightBulbSpawnMessage(
new Transform3D(new Vector3(-10, 2, -5)),
Color.CornflowerBlue,
300f
));
// WorldBuilder.SendMessage(new LightBulbSpawnMessage(
// new Transform3D(new Vector3(-10, 2, -5)),
// Color.CornflowerBlue,
// 300f
// ));
var directionalLightEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(directionalLightEntity, new Transform3DComponent(

View File

@ -1,5 +1,6 @@
using Encompass;
using Microsoft.Xna.Framework;
using MoonTools.Curve;
namespace KavTest.Messages
{
@ -8,12 +9,14 @@ namespace KavTest.Messages
public Transform3D Transform { get; }
public Color Color { get; }
public float Intensity { get; }
public SplineCurve3D Curve { get; }
public LightBulbSpawnMessage(Transform3D transform, Color color, float intensity)
public LightBulbSpawnMessage(Transform3D transform, Color color, float intensity, SplineCurve3D curve)
{
Transform = transform;
Color = color;
Intensity = intensity;
Curve = curve;
}
}
}

1
MoonTools.Curve Submodule

@ -0,0 +1 @@
Subproject commit 1a14f2595ad83dae3d6a715506bf2f735168416c