96 lines
4.3 KiB
C#
96 lines
4.3 KiB
C#
#if UNITY_5_3_OR_NEWER
|
|
using UnityEngine;
|
|
|
|
namespace RoboidControl.Unity {
|
|
public class Wheel : Motor {
|
|
/// <summary>
|
|
/// Create the Unity representation
|
|
/// </summary>
|
|
/// <param name="core">The core motor</param>
|
|
/// <returns>The Unity representation of a motorised wheel</returns>
|
|
public static Wheel Create(RoboidControl.Motor core, float wheelRadius) {
|
|
GameObject prefab = (GameObject)Resources.Load("Wheel");
|
|
if (prefab != null) {
|
|
// Use resource prefab when available
|
|
GameObject gameObj = Instantiate(prefab);
|
|
Wheel component = gameObj.GetComponent<Wheel>();
|
|
if (component != null)
|
|
component.core = core;
|
|
return component;
|
|
}
|
|
else {
|
|
// Fallback implementation
|
|
GameObject gameObj = new(core.name);
|
|
Wheel component = gameObj.AddComponent<Wheel>();
|
|
component.Init(core);
|
|
component.wheelCollider = gameObj.AddComponent<WheelCollider>();
|
|
component.wheelCollider.mass = 0.1f;
|
|
component.wheelCollider.suspensionDistance = 0.01f;
|
|
component.wheelCollider.suspensionSpring = new JointSpring {
|
|
spring = 100f, // Very high spring value to make it rigid
|
|
damper = 10f, // Low damping (could be adjusted for slight 'bounciness')
|
|
targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
};
|
|
Debug.Log("Create " + core.name);
|
|
return component;
|
|
}
|
|
}
|
|
public static Wheel Create(Rigidbody rb, byte thingId) {
|
|
GameObject prefab = (GameObject)Resources.Load("Wheel");
|
|
if (prefab != null) {
|
|
// Use resource prefab when available
|
|
GameObject gameObj = Instantiate(prefab);
|
|
Wheel component = gameObj.GetComponent<Wheel>();
|
|
if (component != null)
|
|
component.core = new RoboidControl.Thing(RoboidControl.Thing.Type.UncontrolledMotor, false);
|
|
return component;
|
|
}
|
|
else {
|
|
// Fallback implementation
|
|
GameObject gameObj = new("Wheel");
|
|
gameObj.transform.parent = rb.transform;
|
|
Wheel component = gameObj.AddComponent<Wheel>();
|
|
SiteServer participant = FindAnyObjectByType<SiteServer>();
|
|
RoboidControl.Thing core = participant.coreParticipant.Get(thingId);
|
|
if (core == null)
|
|
core = new(participant.coreParticipant, RoboidControl.Thing.Type.UncontrolledMotor, thingId, false);
|
|
else {
|
|
;
|
|
}
|
|
component.Init(core);
|
|
component.wheelCollider = gameObj.AddComponent<WheelCollider>();
|
|
component.wheelCollider.mass = 0.1f;
|
|
component.wheelCollider.suspensionDistance = 0.01f;
|
|
component.wheelCollider.suspensionSpring = new JointSpring {
|
|
spring = 100f, // Very high spring value to make it rigid
|
|
damper = 10f, // Low damping (could be adjusted for slight 'bounciness')
|
|
targetPosition = 0.5f // Neutral position (middle of the suspension travel)
|
|
};
|
|
Debug.Log("Create placeholder Wheel ");
|
|
return component;
|
|
}
|
|
}
|
|
|
|
public WheelCollider wheelCollider;
|
|
|
|
protected override void HandlePose() {
|
|
this.wheelCollider.center = core.position.ToVector3();
|
|
this.transform.position = Vector3.zero; // position is done with the center
|
|
}
|
|
|
|
protected override void Update() {
|
|
UpdateThing();
|
|
|
|
if (wheelCollider.radius > 0) {
|
|
float targetRotationSpeed = this.rotationSpeed * 2 * Mathf.PI; // 1 rotation per second in radians
|
|
|
|
// Calculate the required motor torque
|
|
float requiredTorque = (targetRotationSpeed * wheelCollider.mass) / wheelCollider.radius;
|
|
|
|
// Set the motor torque
|
|
wheelCollider.motorTorque = requiredTorque;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |