当前位置:   article > 正文

2.5D瓦块地图坐标转换_unity 2.5d 坐标转换

unity 2.5d 坐标转换

很多瓦块地形都是2.5D的视角呈现世界,在网上查了一些资料,发现从直角坐标变换到2.5D的坐标需做两步:一、旋转45度,二、再把瓦块地图的高压缩一半。

 

 于是乎,第一步旋转后的坐标,比如那个黑色的点的坐标,可以通过计算公式得到:

 然后用C#程序来实现是这个样子:

 C#程序实现如下:

  1. using System.Data;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. namespace _25Dpoint
  7. {
  8. public partial class Form1 : Form
  9. {
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14. private void Form1_Paint(object sender, PaintEventArgs e)
  15. {
  16. Graphics g = this.CreateGraphics();
  17. SolidBrush brushBlack = new SolidBrush(Color.Black);
  18. g.DrawLine(Pens.Black, 200, 0, 200, 400); //画出直角坐标系的x,y轴
  19. g.DrawLine(Pens.Black, 0, 200, 400, 200);
  20. float x1 = 20, y1 = 180; //假设黑点的坐标为20,180 (这个坐标数字是WIN屏幕的坐标,从直角坐标系到WIN屏幕坐标系是不同的,需要转换一下)
  21. Point winpoint1 = screen_point(x1, y1); //直角坐标系到WIN屏幕坐标系
  22. g.DrawString(winpoint1.X.ToString() + "," + winpoint1.Y.ToString(), Font, brushBlack, winpoint1.X, winpoint1.Y); //显示字符串
  23. g.FillEllipse(brushBlack, winpoint1.X, winpoint1.Y, 5, 5); //显示点
  24. float x2 = (float) (x1 * Math.Cos(-45) - y1 * Math.Sin(-45)); //坐标转轴公式 顺时针转45度
  25. float y2 = (float) (x1 * Math.Sin(-45) + y1 * Math.Cos(-45));
  26. Point winpoint2 = screen_point(x2, y2);
  27. g.FillEllipse(brushBlack, winpoint2.X, winpoint2.Y, 5, 5); //显示字符串
  28. g.DrawString(winpoint2.X.ToString() + "," + winpoint2.Y.ToString(), Font, brushBlack, winpoint2.X, winpoint2.Y); //显示转换后的点
  29. }
  30. public Point screen_point(float cartX, float cartY)
  31. {
  32. Point screenpoint = new Point();
  33. screenpoint.X = (int) cartX + ( 400/2 );
  34. screenpoint.Y = (400/2) - (int)cartY;
  35. return screenpoint;
  36. }
  37. }
  38. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/305061
推荐阅读
相关标签
  

闽ICP备14008679号