Document toolboxDocument toolbox

WIP AGV FAQ

Changing the velocity of a motion tensor

Setting the velocity can be achieved by using the ForceVelocity method on the motiontensor.

List<MotionTensor> motionTensors = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensors) { tensor.ForceVelocity(velocity, SimulationController, _intersectionEvent.EventTime); }

Stopping a motion

Setting the velocity of a motiontensor to zero will stop the movement.

List<MotionTensor> motionTensors = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensors) { tensor.ForceVelocity(0, SimulationController, _intersectionEvent.EventTime); }

Reversing a motion

DES supports a negative velocity, to move it in reverse simply use a negative velocity.

List<MotionTensor> motionTensors = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensors) { tensor.ForceVelocity(-velocity, SimulationController, _intersectionEvent.EventTime); }

Getting the current velocity of a motion tensor

The velocity of the motiontensor can be retrieved from the tensorcomponents with the following code.

List<MotionTensor> motionTensors = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensors) { //Get the current velocity velocity = tensor.TensorComponents[0].VelocityAtProgress(0); }

Parent an actor to another actor

To parent an actor the following code can be used.

actor.ChangeTransformParent(_intersectionEvent.EventTime, SimulationController.FrameTime,childActor);

Unparent an actor

To unparent an actor the same code as for parenting an actor can be used. The only difference is that instead of the child it passes null to unparent.

actor.ChangeTransformParent(_intersectionEvent.EventTime, SimulationController.FrameTime,null);

 

Starting a timer

A timer can be created and started with the following code.

//Create the timer to wait Timer timer = new Timer("WaitForAction", secondsToWait, 0, (frameTimePassed, totalFrameTime, revertableTimerEvent) => { //code to run after the timer return true; }); //Start the timer AddActivity(timer); List<DESEvent> _resultingEvents = new List<DESEvent>(); timer.Start(SimulationController,ref _resultingEvents, _intersectionEvent.EventTime, SimulationController.FrameTime); this.SimulationController.RegisterTimelineEventsMidFrame(_resultingEvents);

Destroying an old motion

Destroying an old motion tensor can be achieved with the following code.

List<MotionTensor> motionTensorsBox = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensorsBox) { tensor.Destroy(passedFrameTime, SimulationController); }

Creating a new motion

A new motiontensor can be generated with the following code.

MotionTensorComponent mtc = new MotionTensorComponent(spline, MotionMode.ABSOLUTE, MotionType.TRANSLATION_AND_ROTATION, 0.01d, 1d, 0.9, 0); List<MotionTensorComponent> components = new List<MotionTensorComponent>() {mtc}; MotionTensor motionTensor = new MotionTensor(components); motionTensor.IsRunning = true; SimulationController.AddActivityMidFrame(_intersectionEvent.EventTime, SimulationController.FrameTime, actor, motionTensor);

Executing DES code from outside the DES system

When changing things in the DES system, you have to have the frametime passed and eventtime. Therefore the actions can only be called from inside DES. If you’d like to call these functions from outside of DES, you’ll have to pass them to an instructor and execute them in the next frame with the OnSimulationFrameUpdate.

using System.Collections.Generic; using u040.prespective.prescripted.des; public class ExecuteCodeInstructor : DESInstructor { private bool shouldExecute; public override bool ApplyInstruction(ADESActor actor, ADESCue _cue, ActorCueIntersectionEvent _intersectionEvent) { return false; } public void ShouldExecute() { shouldExecute = true; } public override void OnSimulationFrameUpdate(double _passedFrameTime, double _totalFrameTime, long _frameID, int _framePass, ref List<DESEvent> _resultingEvents, ref List<DESRecord> _resultRecords) { if (_framePass == 1 && shouldExecute) { //Execute your custom code here shouldExecute = false; } base.OnSimulationFrameUpdate(_passedFrameTime, _totalFrameTime, _frameID, _framePass, ref _resultingEvents, ref _resultRecords); } }

Execute code every frame

Because the DES system can run at faster then real time, frame updates should no longer be executed with the FixedUpdate but rather with the override of the OnSimulationFrameUpdate in the DESInstructor.

public override void OnSimulationFrameUpdate(double _passedFrameTime, double _totalFrameTime, long _frameID, int _framePass, ref List<DESEvent> _resultingEvents, ref List<DESRecord> _resultRecords) { if (_framePass == 1) { // Execute your code here } base.OnSimulationFrameUpdate(_passedFrameTime, _totalFrameTime, _frameID, _framePass, ref _resultingEvents, ref _resultRecords); }

Change splines

To change splines, the old motiontensor should be destroyed and a new motiontensor with the new motiontensor should be created.

//Destroy old motion tensor List<MotionTensor> motionTensorsBox = actor.getActivitiesByType<MotionTensor>(); foreach (MotionTensor tensor in motionTensorsBox) { tensor.Destroy(passedFrameTime, SimulationController); } //Create the new motiontensor MotionTensorComponent mtc = new MotionTensorComponent(newSpline, MotionMode.ABSOLUTE, MotionType.TRANSLATION_AND_ROTATION, 0.01d, 1d, 0.9, 0); List<MotionTensorComponent> components = new List<MotionTensorComponent>() {mtc}; MotionTensor motionTensor = new MotionTensor(components); motionTensor.IsRunning = true; SimulationController.AddActivityMidFrame(_intersectionEvent.EventTime, SimulationController.FrameTime, actor, motionTensor);

Generate a motion tensor from code and set the position on the spline from the world position

The motiontensorview has a feature that adds the actor to the closest point on spline when starting the motion. To do the same in code, you can manually calculate the percentage and then create a new motiontensor with that percentage.

//Calculate the closest percentage double percentage = newSpline.GetClosestPercentageToWorldPos(transform.position.ToDouble(), out double _dist, out DVector3 _posOnSpline); //Create the new motiontensor MotionTensorComponent mtc = new MotionTensorComponent(newSpline, MotionMode.ABSOLUTE, MotionType.TRANSLATION_AND_ROTATION, 0.01d, 1d, 0.9, percentage); List<MotionTensorComponent> components = new List<MotionTensorComponent>() {mtc}; MotionTensor motionTensor = new MotionTensor(components); motionTensor.IsRunning = true; SimulationController.AddActivityMidFrame(_intersectionEvent.EventTime, SimulationController.FrameTime, actor, motionTensor);

Prespective Documentation