Code Snippet
private void PopulateWorld()
{
_box1 = AddBox(new Vector3(1, 0.1f, 1));
_box1.State.Name = "box1";
_box1.State.Flags |= EntitySimulationModifiers.Kinematic;
_box2 = AddBox(new Vector3(0, 0.1f, 0));
_box2.State.Name = "box2";
_box2.State.Flags |= EntitySimulationModifiers.Kinematic;
Activate(Arbiter.Receive(false, TimeoutPort(5000), WakeUpBox1));
Activate(Arbiter.Receive(false, TimeoutPort(4000), WakeUpBox2));
}
AnimatedEntity _box1;
AnimatedEntity _box2;
void WakeUpBox1(DateTime now)
{
Waypoint[] waypoints = new Waypoint[]
{
new Waypoint(5f, 0.1f, 0, 0), // start at 5,0.1,0 at 0 seconds
new Waypoint(0, 0.1f, 2, 5) // end at 0,0.1,2 at 5 seconds
};
_box1.SetWaypoints(waypoints);
_box1.StartAnimation();
}
void WakeUpBox2(DateTime now)
{
Waypoint[] waypoints = new Waypoint[]
{
new Waypoint(0, 0.1f, 0, 0), // start at 0,0.1,0 at 0 seconds
new Waypoint(2, 0.1f, 2, 3) // end at 2,0.1,2 at 3 seconds
};
_box2.SetWaypoints(waypoints);
_box2.StartAnimation();
}
public struct Waypoint
{
public float X, Y, Z, T;
public Waypoint(float x, float y, float z, float t)
{
X = x;
Y = y;
Z = z;
T = t;
}
}
public class AnimatedEntity : SingleShapeEntity
{
double _startTime;
bool _animationActive = false;
Waypoint[] _waypoints;
public AnimatedEntity(Shape shape, Vector3 position) : base(shape, position) { }
public void SetWaypoints(Waypoint[] waypoints)
{
_waypoints = waypoints;
}
public void StartAnimation()
{
_startTime = -1; // set this the first time in Update
_animationActive = true;
}
public override void Update(FrameUpdate update)
{
// move the animated entity
if (_animationActive)
{
if (_startTime < 0)
_startTime = update.ApplicationTime;
float i = (float)(update.ApplicationTime - _startTime) / (_waypoints[1].T - _waypoints[0].T);
float oneMinusI = 1f - i;
Vector3 pos = new Vector3();
if (i >= 1)
{
pos.X = _waypoints[1].X;
pos.Y = _waypoints[1].Y;
pos.Z = _waypoints[1].Z;
// finished with animation
_animationActive = false;
}
else
{
pos.X = _waypoints[0].X * oneMinusI + _waypoints[1].X * i;
pos.Y = _waypoints[0].Y * oneMinusI + _waypoints[1].Y * i;
pos.Z = _waypoints[0].Z * oneMinusI + _waypoints[1].Z * i;
}
State.Pose = new Pose(pos);
if (PhysicsEntity != null)
PhysicsEntity.SetPose(State.Pose);
}
base.Update(update);
}
}