gpt4 book ai didi

c# - 立方体内的局部坐标

转载 作者:行者123 更新时间:2023-12-03 19:22:11 26 4
gpt4 key购买 nike

机械臂(对撞机为可动区域)

问题说明

我正在制作机器人 ARM 的虚拟克隆,并需要它移动到立方体(可移动区域)内的坐标。机器人 ARM 现在由三个值控制:x、y 和 z。这些值可以将机器人 ARM 的头部带到立方体的所有四个角。值从 0 到 100。我需要一个统一世界空间中的向量 3 来转换为立方体内的这些值。我这样做是为了让整个机器人预制件可以移动和旋转,而不会破坏这个功能。关于如何实现这一目标有什么想法吗?

最佳答案

如果它是一个简单的立方体,那么您需要知道它的边缘尺寸并执行类似的操作

// Set this e.g. in the Inspector
// or somewhere get it e.g. in Start like
// edgesize = transform.lossyScale.x;
public float edgesize;

public Vector3 RobotToUnityPosition(Vector3 input)
{
var output = input;

// first shift the values in order to map the values (0 to 100) to
// (-50 to +50)
output -= Vector3.one * 50.0f;

// now eliminate the factor 100
// so you get (-0.5 to +0.5)
output /= 100.0f;

// finally scale it accordingly to the cube's edgesize
// so get it mapped to (-0.5 * edgesize to +0.5 * edgesize)
output *= edgesize;

return output;

// could ofcourse also be done in one line like
return ((input - Vector3.one * 50) / 100.0f) * edgesize;
}

public Vector3Int UnityToRobotPosition(Vector3 input)
{
// basically do it the other way round
var output = input;

// Get percentages
// (-0.5 to +0.5)
output /= edgesize;

// scale it up to factor 100
// (-50 to +50)
output *= 100.0f;

// shift the values back
// (0 to 100)
output += Vector3.one * 50.0f;

// Until here you also could have done it again in one line like
//var output = (intput / edgesize) * 100.0f + Vector3.one * 50.0f;

// Now you might want to clamp the values
output.x = Mathf.Clamp(output.x, 0, 100);
output.y = Mathf.Clamp(output.y, 0, 100);
output.z = Mathf.Clamp(output.z, 0, 100);

// Finally you might want to get it as Vector3Int ?
// if not you can skip that and change the return type to Vector3
return Vector3Int.FloorToInt(output);
}

演示(我添加了一些内容只是为了演示它是如何工作的)

enter image description here


如果您想将其用作起点,这是我使用的完整代码

public class PositionConverter : MonoBehaviour
{
public float edgesize;

public Vector3 inputVector;
// I used a transform here in order to simply drag around
// the object in Unity (or set it via script)
public Transform WorldSpacePosition;
public Vector3 backtoRobot;

// whether to automatically update backtoRobot using the WorldSpacePosition.localPosition;
public bool autoupdate;

private void Awake()
{
// I simply used the lossyScale.x as edgeSize
edgesize = transform.lossyScale.x;
}

private void Update()
{
if(!autoupdate)return;

UpdateRobotPosition();
}

// Just fancy stuff for being able to call that method via
// the context menu
[ContextMenu("UpdateWorldPosition")]
private void UpdateWorldPosition()
{
WorldSpacePosition.localPosition = RobotToUnityPosition(inputVector);
}

// Just fancy stuff for being able to call that method via
// the context menu
[ContextMenu("UpdateRobotPosition")]
private void UpdateRobotPosition()
{
backtoRobot = UnityToRobotPosition(WorldSpacePosition.localPosition);
}

public Vector3 RobotToUnityPosition(Vector3 input)
{
var output = input;

// first shift the values since 0 means e.g. -x edge etc
output.x -= Vector3.one * 50.0f;

// eliminate the factor 100
output /= 100.0f;

// scale it according to the cube's edgesize
output *= edgesize;

return output;
}

public Vector3Int UnityToRobotPosition(Vector3 input)
{
// basically do it the other way round
var output = input;

// Get percentages
output /= edgesize;

// scale it up to factor 100
output *= 100;

// shift the values back
output += Vector3.one * 50.0f;

// you might want to clamp the values
output.x = Mathf.Clamp(output.x, 0, 100);
output.y = Mathf.Clamp(output.y, 0, 100);
output.z = Mathf.Clamp(output.z, 0, 100);

// you might want to get it as Vector3Int ?
// if not you can skip that and change the return type to Vector3
return Vector3Int.FloorToInt(output);
}

// Just for drawing the WireCube for the bounds
private void OnDrawGizmos()
{
edgesize = transform.lossyScale.x;
Gizmos.matrix = transform.localToWorldMatrix;

Gizmos.DrawWireCube(Vector3.zero, Vector3.one * edgesize);
}
}
  • 将其添加到空的 GameObject 中,并在 Transform 中设置您想要的比例
  • 添加一个 child ,例如一个 Sphere 对象并将其引用为 WorldSpacePosition

关于c# - 立方体内的局部坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55473483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com