C# Game Engine 模拟游戏 camera/view
C# Game Engine simulating game camera/view
我是新来的,但多年来我遇到问题时都会参考这个网站。你们很有帮助。不幸的是,我找不到想要实现的目标。
我正在用 C# GDI+ 构建一个游戏引擎,而且进展得非常顺利(考虑到 GDI+ 对于游戏来说是多么无能)。
我正在尝试模拟视图。因此,如果视图位于某个位置,则会根据 ViewX
和 ViewY
位置更新游戏对象位置(在 Engine.Loop() 循环中)。例如:
obj.X -= ViewY;
obj.Y -= ViewY;
实际上,这是可行的。视图移动,游戏对象都位于正确的位置。但问题是,当我 return 对象的值时,它们只是 return 那个位置,例如,如果 ViewX 位置设置为 1,对象都会移动,看起来像视图在移动...除了视图不是。
这是游戏循环的代码(不言自明):
//Game loop
if (objects.Count > 0)
{
foreach (GameObject obj in instances)
{
try
{
if (obj.Active) //Only perform this action if the object is currently active
{
if (RectangleInsideView(obj.X, obj.Y, obj.Width, obj.Height))
{
obj.Draw();
}
obj.Step();
//It's because it is constantly updating the object position based on the views position rather than accepting where the view is and
//Just relating it's position to it.
//Eg. if ViewX = 2, obj.X is = obj.X -2...
//We some how need to have a false X and Y variable to set and return...
obj.X = obj.X - ViewX;
obj.Y = obj.Y - ViewY;
}
}
catch (Exception ex) { }
}
}
我知道为什么会这样,但我不知道该如何解决。
我不太明白这个问题,但这听起来不对:
So if the view is at a position, the GameObject position is
updated(In an Engine.Loop() loop) based on the ViewX and ViewY
positions.
obj.X = obj.X - ViewX;
obj.Y = obj.Y - ViewY;
有对象有位置。视图只是绘制对象。如果您使用 GDI+ 开发游戏,您将有很多工作要做!
这是什么东西??
foreach (GameObject obj in instances)
{
if (obj.Active) //Only perform this action if the object is currently active
{
// obj.X -= ViewY and obj.Y -= ViewY;;
GameObject objCopy = view.convertViewIn();
Point[] points = view.convertToScreenCoords(objCopy.getPoints());
Point a = ps[0];
Point b = ps[1];
graphics.DrawLine(usePen, a.X, a.Y, b.X, b.Y);
}
}
public override Point[] convertToScreenCoords(IList<Vector3D> list)
{
Point[] points = new Point[list.Count];
for (int i = 0; i < list.Count; i++)
{
points[i] = convertToScreenCoords(list[i]);
}
return points;
}
public override Point convertToScreenCoords(TheoCAD.Maths.Vector3D coords)
{
return coords.GetTransformedCoord(this.matrixRotateScaleToScreen).toPoint();
}
public Vector3D GetTransformedCoord(Matrix3D matrix)
{
double[] result = new double[4];
double[] temp = new double[4];
temp[0] = this.X;
temp[1] = this.Y;
temp[2] = this.Z;
temp[3] = 1d;
for (int i = 0; i <= 3; i++)
{
result[i] = 0;
for (int j = 0; j <= 3; j++)
{
result[i] += matrix[i, j] * temp[j];
}
}
if (Math.Abs(result[3]) >= 1e-8)
{
result[0] /= result[3];
result[1] /= result[3];
result[2] /= result[3];
}
return new Vector3D(result[0], result[1], result[2]);
}
我会推荐 "Killer Game Programming in Java"。有如何做这样的事情。
http://shop.oreilly.com/product/9780596007300.do
我是新来的,但多年来我遇到问题时都会参考这个网站。你们很有帮助。不幸的是,我找不到想要实现的目标。
我正在用 C# GDI+ 构建一个游戏引擎,而且进展得非常顺利(考虑到 GDI+ 对于游戏来说是多么无能)。
我正在尝试模拟视图。因此,如果视图位于某个位置,则会根据 ViewX
和 ViewY
位置更新游戏对象位置(在 Engine.Loop() 循环中)。例如:
obj.X -= ViewY;
obj.Y -= ViewY;
实际上,这是可行的。视图移动,游戏对象都位于正确的位置。但问题是,当我 return 对象的值时,它们只是 return 那个位置,例如,如果 ViewX 位置设置为 1,对象都会移动,看起来像视图在移动...除了视图不是。
这是游戏循环的代码(不言自明):
//Game loop
if (objects.Count > 0)
{
foreach (GameObject obj in instances)
{
try
{
if (obj.Active) //Only perform this action if the object is currently active
{
if (RectangleInsideView(obj.X, obj.Y, obj.Width, obj.Height))
{
obj.Draw();
}
obj.Step();
//It's because it is constantly updating the object position based on the views position rather than accepting where the view is and
//Just relating it's position to it.
//Eg. if ViewX = 2, obj.X is = obj.X -2...
//We some how need to have a false X and Y variable to set and return...
obj.X = obj.X - ViewX;
obj.Y = obj.Y - ViewY;
}
}
catch (Exception ex) { }
}
}
我知道为什么会这样,但我不知道该如何解决。
我不太明白这个问题,但这听起来不对:
So if the view is at a position, the GameObject position is updated(In an Engine.Loop() loop) based on the ViewX and ViewY positions.
obj.X = obj.X - ViewX;
obj.Y = obj.Y - ViewY;
有对象有位置。视图只是绘制对象。如果您使用 GDI+ 开发游戏,您将有很多工作要做!
这是什么东西??
foreach (GameObject obj in instances)
{
if (obj.Active) //Only perform this action if the object is currently active
{
// obj.X -= ViewY and obj.Y -= ViewY;;
GameObject objCopy = view.convertViewIn();
Point[] points = view.convertToScreenCoords(objCopy.getPoints());
Point a = ps[0];
Point b = ps[1];
graphics.DrawLine(usePen, a.X, a.Y, b.X, b.Y);
}
}
public override Point[] convertToScreenCoords(IList<Vector3D> list)
{
Point[] points = new Point[list.Count];
for (int i = 0; i < list.Count; i++)
{
points[i] = convertToScreenCoords(list[i]);
}
return points;
}
public override Point convertToScreenCoords(TheoCAD.Maths.Vector3D coords)
{
return coords.GetTransformedCoord(this.matrixRotateScaleToScreen).toPoint();
}
public Vector3D GetTransformedCoord(Matrix3D matrix)
{
double[] result = new double[4];
double[] temp = new double[4];
temp[0] = this.X;
temp[1] = this.Y;
temp[2] = this.Z;
temp[3] = 1d;
for (int i = 0; i <= 3; i++)
{
result[i] = 0;
for (int j = 0; j <= 3; j++)
{
result[i] += matrix[i, j] * temp[j];
}
}
if (Math.Abs(result[3]) >= 1e-8)
{
result[0] /= result[3];
result[1] /= result[3];
result[2] /= result[3];
}
return new Vector3D(result[0], result[1], result[2]);
}
我会推荐 "Killer Game Programming in Java"。有如何做这样的事情。 http://shop.oreilly.com/product/9780596007300.do