C#+directX 开发3d游戏 三维坐标转换成屏幕坐标(二维坐标)请各位游戏高手帮忙 Vc++也可以 要详细的

时间:2023-02-02 14:34:50
请高手看看这个三维转二维坐标 是否正确  用的工具是C# 分不是问题
//pt2d类 存储二维坐标
 class pt2d
    {
        double x, y;
        public double X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public double Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }
    }
/////
    ///pt2d 类的索引器
    //////
 class pt2dsuoyin
    {
        pt2d[] pt2ds;
        pt2dsuoyin(int shu)
        {
           pt2ds=new pt2d[shu];

        }
        public pt2d this[int index]
        {
            get
            {
                return pt2ds[index];
            }
            set
            {
                if(index>=0||index <pt2ds.Length)
                {
                    pt2ds[index]=value;
                }
            }
        }
    }
////pt3d 类 存储三维坐标
class pt3d
    {
        double x, y, z;
        public double X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
        public double Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }
        public double Z
        {
            get
            {
                return z ;
            }
            set
            {
                z = value;
            }
        }
    }
///
            ////pt3dshuoyin类    pt3d类的索引器
             ////
/////////
    class pt3dshuoyin
    {
        pt3d[] pt3ds;
        public pt3dshuoyin(int shu)
        {
           pt3ds=new pt3d[shu];
        }
        public pt3d this[int index]
        {
            get
            {
                return pt3ds[index];
 
            }
            set
            {
                if (index >= 0 || index <pt3ds.Length)
                {
                    pt3ds[index]=value;
                }

            }
        }
    }
/////// From3Dto2D类 实现三维转二维坐标
 class From3Dto2D
    {


        public From3Dto2D()
        {

        }
        /// <summary>
        /// 斜等测平行投影  参数是一个三维坐标类                                               T1
        /// </summary>
        /// <param name="t3d"></param>
        /// <returns> 返回一个二维坐标类 用于存储转换后的屏幕坐标</returns>
         public static pt2d Transfrom3DTo2D(pt3d t3d)
        {
            pt2d t2d = new pt2d();
            t2d.X = t3d.X - t3d.Z / Math.Sqrt(2);
            t2d.Y = t3d.Y -t3d .Z / Math.Sqrt(2);
            return t2d;
 
        }

        /// <summary>
         /////方法重写 斜等测平行投影    参数是一个三维坐标 x=x轴 y=y轴 z=轴                     T2
        /// </summary>
        /// <param name="x"></param> 
        /// <param name="y"></param>
        /// <param name="z"></param>
    public    static    pt2d Transform3DTo2D( double x,double y,double z)
        {

            pt2d t2d = new pt2d();     //二维坐标类   用于存储转换后的屏幕坐标
         t2d.X = x - z / Math.Sqrt(2);
         t2d.Y = y - z / Math.Sqrt(2);
         return t2d;
         
        }
        /// <summary>
      ///  方法重写   斜等测平行投影  参数为一个3列数组  0=x轴 1=y轴 2=z轴                        T3
        /// </summary>
        /// <param name="f3d"></param>
   public     static pt2d Transform3DTo2D(double[] f3d)
     {

         pt2d t2d = new pt2d();
         t2d.X = f3d[0] - f3d[2] / Math.Sqrt(2);
         t2d.Y = f3d[1] - f3d[2] / Math.Sqrt(2);
         return t2d;

     }
        /// <summary>
         /// 三维转二维坐标    参数是一个三维坐标类                                                 F1
        /// </summary>
        /// <param name="f3d"></param>
   /// <returns>返回一个二维坐标类 用于存储转换后的屏幕坐标</returns>
   public static pt2d form3DTo2D(pt3d f3d)
   {



       pt2d t2d = new pt2d();
       int D = 200;           //观察者与屏幕的距离
       double ratio;
       ratio = D / (D + f3d.Z);   //透视比
       t2d.X = f3d.X * ratio;  ///x轴
       t2d.Y = f3d.Y * ratio; ///y轴
       return t2d;


   }
        /// <summary>
        ///方法重写 三维转二维坐标    参数是一个三维坐标值  x=x轴 y=轴 z=轴                          F2
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="z"></param>
   /// <returns>//二维坐标类   用于存储转换后的屏幕坐标</returns>
   public      static pt2d form3DTo2D(double x,double y,double z)
        {


            pt2d t2d = new pt2d();
            int D = 200;        //观察者与屏幕的距离
            double ratio;
            ratio = D / (D + z);   //透视比
            t2d.X = x * ratio;
            t2d.Y = y * ratio;
            return t2d;
         
     }
        /// <summary>
        /// 方法重写三维转二维坐标  参数是一个3列的数组  0=x轴 1=y轴 2=z轴                         F3
        /// </summary>
        /// <param name="f3d"></param>
  public    static pt2d form3DTo2D(double[] f3d)
     {



         pt2d t2d = new pt2d();
         int D = 200;        //观察者与屏幕的距离
         double ratio;
         ratio = D / (D + f3d[2]);   //透视比
         t2d.X = f3d[0] * ratio;
         t2d.Y = f3d[1] * ratio;

         return t2d;
     }
       
   }

11 个解决方案

#1


请各位游戏高手帮帮忙谢谢了

#2


because


 x     X
--- = ----
 f     Z


we have

x = X / Z * f
y = Y / Z * f
where (X,Y,Z) is the 3D point, (x,y) is the projected 2d point and f is the focus.

#3


f参数是什么意思

#4


f是焦点是固定值吗??

#5


f是焦点是固定值吗??
yes.

#6


是多少??谢谢了哈哈

#7


我English不好

#8


For example, a camara will have a focus of 28mm ~ 200mm.
The larger the focus length, the bigger your image will be.

If you don't care, any none-zeor f will do, like f = 1

#9


哦哈哈谢谢了我try try thanks了 

#10


这不就是投影矩阵的计算嘛,随便哪本图形学属上都会有的。这和C#无关。

directX对你的这个函数做过实现,原型是Matrix.PerspectiveFovLH,楼主去msdn上查下这个函数是怎么实现的吧

分不是问题,没有分就是问题了,呵呵。

#11


哦哈哈后天结账吧哈哈我是新人 还需要大哥大姐们多多照顾哟哈哈

#1


请各位游戏高手帮帮忙谢谢了

#2


because


 x     X
--- = ----
 f     Z


we have

x = X / Z * f
y = Y / Z * f
where (X,Y,Z) is the 3D point, (x,y) is the projected 2d point and f is the focus.

#3


f参数是什么意思

#4


f是焦点是固定值吗??

#5


f是焦点是固定值吗??
yes.

#6


是多少??谢谢了哈哈

#7


我English不好

#8


For example, a camara will have a focus of 28mm ~ 200mm.
The larger the focus length, the bigger your image will be.

If you don't care, any none-zeor f will do, like f = 1

#9


哦哈哈谢谢了我try try thanks了 

#10


这不就是投影矩阵的计算嘛,随便哪本图形学属上都会有的。这和C#无关。

directX对你的这个函数做过实现,原型是Matrix.PerspectiveFovLH,楼主去msdn上查下这个函数是怎么实现的吧

分不是问题,没有分就是问题了,呵呵。

#11


哦哈哈后天结账吧哈哈我是新人 还需要大哥大姐们多多照顾哟哈哈