浏览代码

more cleanup

/active-variablespeed
HH 4 年前
当前提交
7979a7a3
共有 3 个文件被更改,包括 0 次插入382 次删除
  1. 37
      Project/Assets/ML-Agents/Examples/Walker/Scenes/WalkerDynamicVariableSpeed.unity
  2. 334
      Project/Assets/ML-Agents/Examples/Walker/Scripts/AvgCenterOfMass.cs
  3. 11
      Project/Assets/ML-Agents/Examples/Walker/Scripts/AvgCenterOfMass.cs.meta

37
Project/Assets/ML-Agents/Examples/Walker/Scenes/WalkerDynamicVariableSpeed.unity


objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 3ce107b4a79bc4eef83afde434932a68, type: 3}
--- !u!1 &2066646069 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 7175173927432230581, guid: 4ee4867f326424260aa2753712f3d643,
type: 3}
m_PrefabInstance: {fileID: 1359243704}
m_PrefabAsset: {fileID: 0}
--- !u!114 &2066646077
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2066646069}
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 74cc05b039aa04d3da17ac9921123c22, type: 3}
m_Name:
m_EditorClassIdentifier:
active: 0
showCOMGizmos: 1
avgCOMWorldSpace: {x: 0, y: 2.9995303, z: 0.0034782607}
avgCOMVelocityWorldSpace: {x: 0, y: 0, z: 0}
previousAvgCOM: {x: 0, y: 0, z: 0}
avgCOMColor: {r: 0, g: 1, b: 0, a: 1}
bodyPartCOMColor: {r: 1, g: 0.92156863, b: 0.015686275, a: 1}
totalMass: 115
showBPPosRelToBody: 0
useTransformPoint: 0
useTransformVector: 0
useTransformDir: 0
showRBPos: 0
showRelPosVectorOnly: 0
showInverseTransformPointUnscaledRelToBody: 0
showInverseTransformPointRelToBody: 0
showInverseTransformVectorRelToBody: 0
showInverseTransformDirRelToBody: 0
body_T: {fileID: 0}
--- !u!1 &2095421678
GameObject:
m_ObjectHideFlags: 0

334
Project/Assets/ML-Agents/Examples/Walker/Scripts/AvgCenterOfMass.cs


using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Used for visualizing the average center of mass of a ragdoll
/// </summary>
[DisallowMultipleComponent]
[ExecuteInEditMode]
public class AvgCenterOfMass : MonoBehaviour
{
/// <summary>
/// Enable to show a green spehere at the current center of mass.
/// </summary>
[Tooltip("Enable to show a green spehere at the current center of mass.")]
public bool active;
public bool showCOMGizmos = true;
public Vector3 avgCOMWorldSpace;
public Vector3 avgCOMVelocityWorldSpace;
public Vector3 previousAvgCOM;
public Color avgCOMColor = Color.green;
public Color bodyPartCOMColor = Color.yellow;
List<Rigidbody> rbList = new List<Rigidbody>();
public float totalMass;
[Tooltip("Visualize Relative Pos")]
public bool showBPPosRelToBody;
public bool useTransformPoint = true;
public bool useTransformVector;
public bool useTransformDir;
public bool showRBPos;
public bool showRelPosVectorOnly;
public bool showInverseTransformPointUnscaledRelToBody;
public bool showInverseTransformPointRelToBody;
public bool showInverseTransformVectorRelToBody;
public bool showInverseTransformDirRelToBody;
public Transform body_T;
void OnEnable()
{
SetUpRigidbodies();
}
void SetUpRigidbodies()
{
rbList.Clear();
totalMass = 0;
foreach(var item in GetComponentsInChildren<Rigidbody>())
{
rbList.Add(item);
totalMass += item.mass;
}
}
// void FixedUpdate()
// {
// if(Application.isPlaying)
// {
// avgCOMWorldSpace = Vector3.zero;
// foreach(var item in rbList)
// {
// if (item)
// {
// avgCOMWorldSpace += item.worldCenterOfMass;
// }
// }
// //DRAW AVG GIZMOS
// avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
// }
// }
public Vector3 GetCoMWorldSpace()
{
Vector3 CoM = Vector3.zero;
avgCOMWorldSpace = Vector3.zero;
float c = 0f;
foreach(var item in rbList)
{
CoM += item.worldCenterOfMass * item.mass;
c += item.mass;
}
avgCOMWorldSpace = CoM/c;
avgCOMVelocityWorldSpace = (avgCOMWorldSpace - previousAvgCOM)/Time.fixedDeltaTime;
// Debug.DrawRay(avgCOMWorldSpace, avgCOMVelocityWorldSpace, Color.green,Time.fixedDeltaTime);
// Debug.DrawRay(avgCOMWorldSpace, Vector3.ProjectOnPlane( avgCOMVelocityWorldSpace, Vector3.up), Color.green,Time.fixedDeltaTime);
previousAvgCOM = avgCOMWorldSpace;
return avgCOMWorldSpace;
}
void FixedUpdate()
{
if(Application.isPlaying)
{
// avgCOMWorldSpace = Vector3.zero;
// foreach(var item in rbList)
// {
// if (item)
// {
// avgCOMWorldSpace += item.worldCenterOfMass;
// }
// }
// //DRAW AVG GIZMOS
// avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
// if (active)
// {
GetCoMWorldSpace();
// }
// Vector3 CoM = Vector3.zero;
// avgCOMWorldSpace = Vector3.zero;
// float c = 0f;
//
// foreach(var item in rbList)
// {
// CoM += item.worldCenterOfMass * item.mass;
// c += item.mass;
// }
// avgCOMWorldSpace = CoM/c;
// avgCOMVelocityWorldSpace = previousAvgCOM - avgCOMWorldSpace;
// Debug.DrawRay(avgCOMWorldSpace, avgCOMVelocityWorldSpace, Color.green,Time.fixedDeltaTime);
//
// previousAvgCOM = avgCOMWorldSpace;
// // CoM /= c;
//
if (showBPPosRelToBody)
{
var pos = body_T.position;
Matrix4x4 bodyMatrix = body_T.localToWorldMatrix;
// get position from the last column
var bodyPos = new Vector3(bodyMatrix[0,3], bodyMatrix[1,3], bodyMatrix[2,3]);
Debug.DrawRay(bodyPos, Vector3.up, Color.yellow,Time.fixedDeltaTime);
foreach (var rb in rbList)
{
if (showRBPos)
{
Debug.DrawRay(rb.position, Vector3.up, Color.green,Time.fixedDeltaTime);
}
if (rb.transform != body_T)
{
if (showRelPosVectorOnly)
{
var relPosVector = rb.position - body_T.position;
// Debug.DrawRay(body_T.position + body_T.InverseTransformPoint(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
Debug.DrawRay(body_T.position + relPosVector, Vector3.up, Color.red,Time.fixedDeltaTime);
// Vector3 currentLocalPosRelToMatrix = bodyMatrix.inverse.MultiplyPoint(rb.position);
Vector3 currentLocalPosRelToMatrix = bodyMatrix.inverse.MultiplyVector(rb.position - bodyPos);
Debug.DrawRay(body_T.position + currentLocalPosRelToMatrix, Vector3.up, Color.green,Time.fixedDeltaTime);
}
if (showInverseTransformPointUnscaledRelToBody)
{
// Debug.DrawRay(body_T.position + body_T.InverseTransformPoint(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
// Debug.DrawRay(body_T.position + body_T.InverseTransformPointUnscaled(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
//disabled this
// Debug.DrawRay(body_T.position + body_T.InverseTransformPointUnscaled(rb.transform.position), Vector3.up, Color.red,Time.fixedDeltaTime);
}
if (showInverseTransformPointRelToBody)
{
// Debug.DrawRay(body_T.position + body_T.InverseTransformPoint(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
Debug.DrawRay(body_T.position + body_T.InverseTransformPoint(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
}
if (showInverseTransformDirRelToBody)
{
Debug.DrawRay(body_T.InverseTransformDirection(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
}
if (showInverseTransformVectorRelToBody)
{
Debug.DrawRay(body_T.position + body_T.InverseTransformVector(rb.position - body_T.position), Vector3.up, Color.red,Time.fixedDeltaTime);
}
// var localPosRelToBody = body.InverseTransformPoint(rb.position);
// Debug.DrawRay(body_T.position + body_T.InverseTransformPoint(rb.position), Vector3.up, Color.red,Time.fixedDeltaTime);
// Debug.DrawRay(body_T.position + rb.transform.TransformVector(rb.transform.localPosition), Vector3.up, Color.cyan,Time.fixedDeltaTime);
// Debug.DrawRay(rb.transform.TransformPoint(rb.position), Vector3.up, Color.green,Time.fixedDeltaTime);
// Debug.DrawRay(body_T.position + body_T.InverseTransformVector(rb.transform.position), Vector3.up, Color.red,Time.fixedDeltaTime);
// Debug.DrawRay(body_T.position + body_T.InverseTransformDirection(rb.transform.position), Vector3.up, Color.red,Time.fixedDeltaTime);
// Debug.DrawRay(body_T.position + body_T.TransformPoint(rb.transform.localPosition), Vector3.up, Color.red,Time.fixedDeltaTime);
if (useTransformPoint)
{
}
else if (useTransformVector)
{
}
else if (useTransformDir)
{
}
}
}
}
}
}
// private void OnDrawGizmosSelected()
private void OnDrawGizmos()
{
if(!Application.isPlaying)
{
if (showCOMGizmos)
{
Vector3 CoM = Vector3.zero;
float c = 0f;
// avgCOMWorldSpace = Vector3.zero;
//SHOW BODY PART GIZMOS
foreach(var item in rbList)
{
// if (item)
// {
Gizmos.color = bodyPartCOMColor;
float drawCOMRadius = item.mass/totalMass;
Gizmos.DrawSphere(item.worldCenterOfMass, drawCOMRadius);
// Gizmos.DrawWireSphere(item.worldCenterOfMass, drawCOMRadius);
CoM += item.worldCenterOfMass * item.mass;
c += item.mass;
// avgCOMWorldSpace += item.worldCenterOfMass;
// }
}
//DRAW AVG GIZMOS
avgCOMWorldSpace = CoM/c;
// avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
float avgCOMRadius = 0.5f; //radius of gizmo
Gizmos.color = avgCOMColor;
Gizmos.DrawSphere(avgCOMWorldSpace, avgCOMRadius);
}
}
else
{
if (showCOMGizmos)
{
// avgCOMWorldSpace = Vector3.zero;
//SHOW BODY PART GIZMOS
foreach(var item in rbList)
{
// if (item)
// {
Gizmos.color = bodyPartCOMColor;
float drawCOMRadius = item.mass/totalMass;
Gizmos.DrawSphere(item.worldCenterOfMass, drawCOMRadius);
// Gizmos.DrawWireSphere(item.worldCenterOfMass, drawCOMRadius);
// avgCOMWorldSpace += item.worldCenterOfMass;
// }
}
//DRAW AVG GIZMOS
// avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
float avgCOMGizmoRadius = 0.5f; //radius of gizmo
Gizmos.color = avgCOMColor;
Gizmos.DrawSphere(avgCOMWorldSpace, avgCOMGizmoRadius);
}
}
}
// {
// if(!Application.isPlaying)
// {
// if (showCOMGizmos)
// {
// avgCOMWorldSpace = Vector3.zero;
// //SHOW BODY PART GIZMOS
// foreach(var item in rbList)
// {
// if (item)
// {
// Gizmos.color = bodyPartCOMColor;
// float drawCOMRadius = item.mass/totalMass;
// Gizmos.DrawWireSphere(item.worldCenterOfMass, drawCOMRadius);
// avgCOMWorldSpace += item.worldCenterOfMass;
// }
// }
// //DRAW AVG GIZMOS
// avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
// float avgCOMRadius = 0.1f; //radius of gizmo
// Gizmos.color = avgCOMColor;
// Gizmos.DrawSphere(avgCOMWorldSpace, avgCOMRadius);
// }
// }
// else
// {
// if (showCOMGizmos)
// {
// // avgCOMWorldSpace = Vector3.zero;
// //SHOW BODY PART GIZMOS
// foreach(var item in rbList)
// {
// if (item)
// {
// Gizmos.color = bodyPartCOMColor;
// float drawCOMRadius = item.mass/totalMass;
// Gizmos.DrawWireSphere(item.worldCenterOfMass, drawCOMRadius);
// // avgCOMWorldSpace += item.worldCenterOfMass;
// }
// }
// //DRAW AVG GIZMOS
// // avgCOMWorldSpace /= rbList.Count; //divide by num of rb's to get avg in WORLD space
// float avgCOMGizmoRadius = 0.1f; //radius of gizmo
// Gizmos.color = avgCOMColor;
// Gizmos.DrawSphere(avgCOMWorldSpace, avgCOMGizmoRadius);
// }
// }
// }
}

11
Project/Assets/ML-Agents/Examples/Walker/Scripts/AvgCenterOfMass.cs.meta


fileFormatVersion: 2
guid: 74cc05b039aa04d3da17ac9921123c22
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存