-
Notifications
You must be signed in to change notification settings - Fork 4
/
VehicleScript.cs
112 lines (78 loc) · 4.04 KB
/
VehicleScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using Xenko.Core;
using Xenko.Core.Mathematics;
using Xenko.Engine;
using Xenko.Input;
using Xenko.Physics;
namespace XenkoByteSized {
public class VehicleScript : SyncScript {
[Display("Wheel Front Left")]
public RigidbodyComponent wheelFrontLeft;
private Generic6DoFConstraint wheelFrontLeftConstraint;
[Display("Wheel Front Right")]
public RigidbodyComponent wheelFrontRight;
private Generic6DoFConstraint wheelFrontRightConstraint;
[Display("Wheel Back Left")]
public RigidbodyComponent wheelBackLeft;
private Generic6DoFConstraint wheelBackLeftConstraint;
[Display("Wheel Back Right")]
public RigidbodyComponent wheelBackRight;
private Generic6DoFConstraint wheelBackRightConstraint;
private RigidbodyComponent vehicleBody;
public override void Start() {
vehicleBody = Entity.Get<RigidbodyComponent>();
SetupPhysicsConstraints();
}
static private Generic6DoFConstraint CreateWheelConstraint(RigidbodyComponent parent, RigidbodyComponent wheel) {
Matrix wheelTranslation = Matrix.Translation(wheel.Entity.Transform.Position);
Quaternion wheelRotation = wheel.Entity.Transform.Rotation;
Generic6DoFConstraint wheelConstraint = Simulation.CreateConstraint(
ConstraintTypes.Generic6DoF, parent, wheel,
wheelTranslation, /* the constraint itself should be created at the offset where the wheel itself is */
Matrix.RotationQuaternion(wheelRotation),
useReferenceFrameA : true
) as Generic6DoFConstraint;
/* lock X and Y rotation */
wheelConstraint.AngularLowerLimit = new Vector3(0.0f, 0.0f, -(float)Math.PI);
wheelConstraint.AngularUpperLimit = new Vector3(0.0f, 0.0f, (float)Math.PI);
return wheelConstraint;
}
private void SetupPhysicsConstraints() {
var sim = this.GetSimulation();
Generic6DoFConstraint wheel1Constraint = CreateWheelConstraint(vehicleBody, wheelFrontLeft);
Generic6DoFConstraint wheel2Constraint = CreateWheelConstraint(vehicleBody, wheelFrontRight);
Generic6DoFConstraint wheel3Constraint = CreateWheelConstraint(vehicleBody, wheelBackLeft);
Generic6DoFConstraint wheel4Constraint = CreateWheelConstraint(vehicleBody, wheelBackRight);
sim.AddConstraint(wheel1Constraint);
wheelFrontLeftConstraint = wheel1Constraint;
sim.AddConstraint(wheel2Constraint);
wheelFrontRightConstraint = wheel2Constraint;
sim.AddConstraint(wheel3Constraint);
wheelBackLeftConstraint = wheel3Constraint;
sim.AddConstraint(wheel4Constraint);
wheelBackRightConstraint = wheel4Constraint;
}
public override void Update() {
float dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;
const float FORCE_PER_SECOND = 10;
if (Input.IsKeyDown(Keys.Right)) {
vehicleBody.ApplyImpulse(Entity.Transform.WorldMatrix.Left * FORCE_PER_SECOND * dt);
} else if (Input.IsKeyDown(Keys.Left)) {
vehicleBody.ApplyImpulse(Entity.Transform.WorldMatrix.Right * FORCE_PER_SECOND * dt);
}
DebugText.Print("Left/Right Arrow Key to move the vehicle", new Int2(32, 32));
}
public override void Cancel() {
/* make sure to remove constraints and also dispose them! */
var sim = this.GetSimulation();
sim.RemoveConstraint(wheelFrontLeftConstraint);
sim.RemoveConstraint(wheelFrontRightConstraint);
sim.RemoveConstraint(wheelBackLeftConstraint);
sim.RemoveConstraint(wheelBackRightConstraint);
wheelFrontLeftConstraint.Dispose();
wheelFrontRightConstraint.Dispose();
wheelBackLeftConstraint.Dispose();
wheelBackRightConstraint.Dispose();
}
}
}