目前,我让玩家在地图中选择2个位置,这是一个3d世界,但我的问题仅与前2个维度相关。我想在2个选定点之间创建一个地板,并将它们旋转到这两个点之间的角度。 http://i.imgur.com/hCjtEzB.png(请原谅我的绘画技巧)
我的代码在某些情况下可以正常工作,但在其他情况下,条件箱会以错误的角度生成。我认为这与vec1.x>有关。 vec2.x,或类似的东西。但我无法理解。
我错过了什么?
const int modelLength = 55;
const int modelWidth = 30;
void createFloor(Vector vec1, Vector vec2)
{
float height = vec1.Z;
float length = Math.Abs(vec1.X - vec2.X);
float width = Math.Abs(vec1.Y - vec2.Y);
int crateWidth = (int)Math.Ceiling(width / modelWidth);
int crateLength = (int)Math.Ceiling(length / modelLength);
int adjustedWidth = crateWidth * modelWidth;
int adjustedLength = crateLength * modelLength;
double angleRad = Math.Atan2(adjustedWidth, adjustedLength);
double angleDeg = Util.RadianToDegree(angleRad);
Vector angles = new Vector(0, (float)angleDeg, 0);
float mX = (vec1.X < vec2.X) ? adjustedLength / 2 + vec1.X : vec1.X - (adjustedLength / 2);
float mY = (vec1.Y < vec2.Y) ? adjustedWidth / 2 + vec1.Y : vec1.Y - (adjustedWidth / 2);
Vector middle = new Vector(mX, mY, vec1.Z);
for (int i = 0; i < crateLength; i++)
{
for (int j = 0; j < crateWidth; j++)
{
float x = (vec1.X < vec2.X) ? vec1.X + i * modelLength : vec1.X - i * modelLength;
float y = (vec1.Y < vec2.Y) ? vec1.Y + j * modelWidth : vec1.Y - j * modelWidth;
Vector v = new Vector(x, y, height);
v = Util.RotateAround(v, middle, angleDeg);
spawnCrate(v, angles);
}
}
}
public static Vector RotateAround(Vector vectorToRotate, Vector center, double angleDeg)
{
double angleRad = DegreeToRadian(angleDeg);
double cosTheta = Math.Cos(angleRad);
double sinTheta = Math.Sin(angleRad);
return new Vector
{
X = (int)(cosTheta * (vectorToRotate.X - center.X) - sinTheta * (vectorToRotate.Y - center.Y) + center.X),
Y = (int)(sinTheta * (vectorToRotate.X - center.X) + cosTheta * (vectorToRotate.Y - center.Y) + center.Y),
Z = vectorToRotate.Z
};
}
感谢。