Debug raycast now prints rotation to align to normal

This commit is contained in:
Nick 2022-11-06 21:24:55 -05:00
parent 965136505f
commit 8f52a9a528
4 changed files with 17 additions and 31 deletions

View File

@ -225,7 +225,7 @@ namespace NewHorizons.Utility.DebugMenu
else if (conversationMeta.conversationGo != null)
{
conversationMeta.conversationGo.transform.localPosition = data.pos;
DebugPropPlacer.SetGameObjectRotation(conversationMeta.conversationGo, data, _dnp.gameObject.transform.position);
conversationMeta.conversationGo.transform.rotation = data.rot;
conversationMeta.conversation.position = conversationMeta.conversationGo.transform.localPosition;
conversationMeta.conversation.rotation = conversationMeta.conversationGo.transform.localEulerAngles;

View File

@ -106,7 +106,7 @@ namespace NewHorizons.Utility.DebugUtilities
internal void PlaceObject()
{
DebugRaycastData data = _rc.Raycast();
PlaceObject(data, this.gameObject.transform.position);
PlaceObject(data);
if (!hasAddedCurrentObjectToRecentsList)
{
@ -119,7 +119,7 @@ namespace NewHorizons.Utility.DebugUtilities
}
}
public void PlaceObject(DebugRaycastData data, Vector3 playerAbsolutePosition)
public void PlaceObject(DebugRaycastData data)
{
// TODO: implement sectors
// if this hits a sector, store that sector and add a config file option for it
@ -149,14 +149,12 @@ namespace NewHorizons.Utility.DebugUtilities
var detailInfo = new PropModule.DetailInfo()
{
position = data.pos,
rotation = data.norm,
rotation = data.rot.eulerAngles,
};
var prop = DetailBuilder.Make(planetGO, sector, prefab, detailInfo);
var body = data.hitBodyGameObject.GetComponent<AstroObject>();
if (body != null) RegisterProp(body, prop);
SetGameObjectRotation(prop, data, playerAbsolutePosition);
}
catch
{
@ -164,28 +162,7 @@ namespace NewHorizons.Utility.DebugUtilities
}
}
public static void SetGameObjectRotation(GameObject prop, DebugRaycastData data, Vector3 playerAbsolutePosition)
{
// align with surface normal
Vector3 alignToSurface = (Quaternion.LookRotation(data.norm) * Quaternion.FromToRotation(Vector3.up, Vector3.forward)).eulerAngles;
prop.transform.localEulerAngles = alignToSurface;
// rotate facing dir towards player
GameObject g = new GameObject("DebugProp");
g.transform.parent = prop.transform.parent;
g.transform.localPosition = prop.transform.localPosition;
g.transform.localRotation = prop.transform.localRotation;
prop.transform.parent = g.transform;
var dirTowardsPlayer = prop.transform.parent.transform.InverseTransformPoint(playerAbsolutePosition) - prop.transform.localPosition;
dirTowardsPlayer.y = 0;
float rotation = Quaternion.LookRotation(dirTowardsPlayer).eulerAngles.y;
prop.transform.localEulerAngles = new Vector3(0, rotation, 0);
prop.transform.parent = g.transform.parent;
GameObject.Destroy(g);
}
public static string GetAstroObjectName(string bodyName)
{

View File

@ -12,6 +12,7 @@ namespace NewHorizons.Utility.DebugUtilities
public bool hit;
public Vector3 pos;
public Vector3 norm;
public Quaternion rot;
public DebugRaycastPlane plane;
public string colliderPath;

View File

@ -1,3 +1,5 @@
using NewHorizons.Builder.Props;
using NewHorizons.External.Modules;
using NewHorizons.Handlers;
using UnityEngine;
using UnityEngine.InputSystem;
@ -61,6 +63,7 @@ namespace NewHorizons.Utility.DebugUtilities
}
}
internal string Vector3ToString(Vector3 v) => $"{{\"x\": {v.x}, \"y\": {v.y}, \"z\": {v.z}}}";
internal void PrintRaycast()
{
@ -72,8 +75,9 @@ namespace NewHorizons.Utility.DebugUtilities
return;
}
var posText = $"{{\"x\": {data.pos.x}, \"y\": {data.pos.y}, \"z\": {data.pos.z}}}";
var normText = $"{{\"x\": {data.norm.x}, \"y\": {data.norm.y}, \"z\": {data.norm.z}}}";
var posText = Vector3ToString(data.pos);
var normText = Vector3ToString(data.norm);
var rotText = Vector3ToString(data.rot.eulerAngles);
if(_surfaceSphere != null) GameObject.Destroy(_surfaceSphere);
if(_normalSphere1 != null) GameObject.Destroy(_normalSphere1);
@ -104,7 +108,7 @@ namespace NewHorizons.Utility.DebugUtilities
_planeDownLeftSphere .transform.localPosition = data.plane.origin + data.plane.u*-1*planeSize + data.plane.v*-1*planeSize;
_planeDownRightSphere.transform.localPosition = data.plane.origin + data.plane.u*1*planeSize + data.plane.v*-1*planeSize;
Logger.Log($"Raycast hit \"position\": {posText}, \"normal\": {normText} on collider [{data.colliderPath}] " +
Logger.Log($"Raycast hit\n\n\"position\": {posText},\n\"rotation\": {rotText},\n\"normal\": {normText}\n\non collider [{data.colliderPath}] " +
(data.bodyPath != null? $"at rigidbody [{data.bodyPath}]" : "not attached to a rigidbody"));
}
internal DebugRaycastData Raycast()
@ -121,6 +125,7 @@ namespace NewHorizons.Utility.DebugUtilities
{
data.pos = hitInfo.transform.InverseTransformPoint(hitInfo.point);
data.norm = hitInfo.transform.InverseTransformDirection(hitInfo.normal);
var o = hitInfo.transform.gameObject;
var hitAstroObject = o.GetComponent<AstroObject>() ?? o.GetComponentInParent<AstroObject>();
@ -130,13 +135,16 @@ namespace NewHorizons.Utility.DebugUtilities
data.hitObject = o;
data.hitBodyGameObject = hitAstroObject?.gameObject ?? o;
data.plane = ConstructPlane(data);
var toPlayer = Vector3.ProjectOnPlane((transform.position - hitInfo.point).normalized, hitInfo.normal);
var worldSpaceRot = Quaternion.LookRotation(toPlayer, hitInfo.normal);
data.rot = hitAstroObject.transform.InverseTransformRotation(worldSpaceRot);
}
_rb.EnableCollisionDetection();
return data;
}
internal DebugRaycastPlane ConstructPlane(DebugRaycastData data)
{
var U = data.pos - Vector3.zero; // U is the local "up" direction. the direction directly away from the center of the planet at this point. // pos is always relative to the body, so the body is considered to be at 0,0,0.