浏览代码

update crawler code

/hh-develop-ragdoll-testing
HH 5 年前
当前提交
b793d93a
共有 2 个文件被更改,包括 89 次插入32 次删除
  1. 120
      Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs
  2. 1
      Project/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs

120
Project/Assets/ML-Agents/Examples/Crawler/Scripts/CrawlerAgent.cs


[Space(10)]
public Transform target;
[Space(10)]
[Header("Orientation Cube")]
[Space(10)]
//This will be used as a stable observation platform for the ragdoll to use.
GameObject m_OrientationCube;
public Transform ground;
public bool detectTargets;
public bool targetIsStatic;

public override void Initialize()
{
//Spawn an orientation cube
Vector3 oCubePos = hips.position;
oCubePos.y = -.45f;
m_OrientationCube = Instantiate(Resources.Load<GameObject>("OrientationCube"), oCubePos, Quaternion.identity);
m_OrientationCube.transform.SetParent(transform);
//Setup each body part
m_JdController.SetupBodyPart(body);

m_JdController.SetupBodyPart(leg3Upper);
m_JdController.SetupBodyPart(leg3Lower);
}
/// <summary>
/// Add relevant information on each body part to observations.
/// </summary>

sensor.AddObservation(bp.groundContact.touchingGround ? 1 : 0); // Whether the bp touching the ground
var velocityRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(rb.velocity);
sensor.AddObservation(velocityRelativeToLookRotationToTarget);
var angularVelocityRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(rb.angularVelocity);
sensor.AddObservation(angularVelocityRelativeToLookRotationToTarget);
//Get velocities in the context of our orientation cube's space
//Note: You can get these velocities in world space as well but it may not train as well.
sensor.AddObservation(m_OrientationCube.transform.InverseTransformDirection(bp.rb.velocity));
sensor.AddObservation(m_OrientationCube.transform.InverseTransformDirection(bp.rb.angularVelocity));
//Get position relative to hips in the context of our orientation cube's space
sensor.AddObservation(m_OrientationCube.transform.InverseTransformDirection(bp.rb.position - body.position));
var localPosRelToBody = body.InverseTransformPoint(rb.position);
sensor.AddObservation(localPosRelToBody);
sensor.AddObservation(bp.currentXNormalizedRot); // Current x rot
sensor.AddObservation(bp.currentYNormalizedRot); // Current y rot
sensor.AddObservation(bp.currentZNormalizedRot); // Current z rot
sensor.AddObservation(bp.rb.transform.localRotation);
// /// <summary>
// /// Add relevant information on each body part to observations.
// /// </summary>
// public void CollectObservationBodyPart(BodyPart bp, VectorSensor sensor)
// {
// var rb = bp.rb;
// sensor.AddObservation(bp.groundContact.touchingGround ? 1 : 0); // Whether the bp touching the ground
//
// var velocityRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(rb.velocity);
// sensor.AddObservation(velocityRelativeToLookRotationToTarget);
//
// var angularVelocityRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(rb.angularVelocity);
// sensor.AddObservation(angularVelocityRelativeToLookRotationToTarget);
//
// if (bp.rb.transform != body)
// {
// var localPosRelToBody = body.InverseTransformPoint(rb.position);
// sensor.AddObservation(localPosRelToBody);
// sensor.AddObservation(bp.currentXNormalizedRot); // Current x rot
// sensor.AddObservation(bp.currentYNormalizedRot); // Current y rot
// sensor.AddObservation(bp.currentZNormalizedRot); // Current z rot
// sensor.AddObservation(bp.currentStrength / m_JdController.maxJointForceLimit);
// }
// }
/// <summary>
/// Loop over body parts to add them to observation.
/// </summary>
m_JdController.GetCurrentJointForces();
// Update pos to target
m_DirToTarget = target.position - body.position;
m_LookRotation = Quaternion.LookRotation(m_DirToTarget);
m_TargetDirMatrix = Matrix4x4.TRS(Vector3.zero, m_LookRotation, Vector3.one);
sensor.AddObservation(Quaternion.FromToRotation(body.forward, m_OrientationCube.transform.forward));
sensor.AddObservation(m_OrientationCube.transform.InverseTransformPoint(target.position));
if (Physics.Raycast(body.position, Vector3.down, out hit, 10.0f))
float maxRaycastDist = 10;
if (Physics.Raycast(body.position, Vector3.down, out hit, maxRaycastDist))
sensor.AddObservation(hit.distance);
sensor.AddObservation(hit.distance/maxRaycastDist);
sensor.AddObservation(10.0f);
// Forward & up to help with orientation
var bodyForwardRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.forward);
sensor.AddObservation(bodyForwardRelativeToLookRotationToTarget);
var bodyUpRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.up);
sensor.AddObservation(bodyUpRelativeToLookRotationToTarget);
foreach (var bodyPart in m_JdController.bodyPartsDict.Values)
sensor.AddObservation(maxRaycastDist);
foreach (var bodyPart in m_JdController.bodyPartsList)
// public override void CollectObservations(VectorSensor sensor)
// {
// m_JdController.GetCurrentJointForces();
//
// // Update pos to target
// m_DirToTarget = target.position - body.position;
// m_LookRotation = Quaternion.LookRotation(m_DirToTarget);
// m_TargetDirMatrix = Matrix4x4.TRS(Vector3.zero, m_LookRotation, Vector3.one);
//
// RaycastHit hit;
// if (Physics.Raycast(body.position, Vector3.down, out hit, 10.0f))
// {
// sensor.AddObservation(hit.distance);
// }
// else
// sensor.AddObservation(10.0f);
//
// // Forward & up to help with orientation
// var bodyForwardRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.forward);
// sensor.AddObservation(bodyForwardRelativeToLookRotationToTarget);
//
// var bodyUpRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.up);
// sensor.AddObservation(bodyUpRelativeToLookRotationToTarget);
//
// foreach (var bodyPart in m_JdController.bodyPartsDict.Values)
// {
// CollectObservationBodyPart(bodyPart, sensor);
// }
// }
/// <summary>
/// Agent touched the target

1
Project/Assets/ML-Agents/Examples/Walker/Scripts/WalkerAgent.cs


sensor.AddObservation(Quaternion.FromToRotation(hips.forward, m_OrientationCube.transform.forward));
sensor.AddObservation(Quaternion.FromToRotation(head.forward, m_OrientationCube.transform.forward));
//clamp the distance vector in case the target is far away. normalized to 1.
sensor.AddObservation(m_OrientationCube.transform.InverseTransformPoint(target.position));
foreach (var bodyPart in m_JdController.bodyPartsList)

正在加载...
取消
保存