游戏开发中的数学和物理算法(17):平移

本文介绍了游戏开发中实现平移的基本数学原理,包括2D和3D空间内的加法及乘法平移方法,并提供了具体的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游戏开发中的数学和物理算法(17):平移

点的平移,可以通过矩阵相加相乘来模拟。

2D加法平移:

例如:
点A(20,30),向右移50个像素,向下移100个像素,得到A'。

那么A'(70,-70)。

3D加法平移:

例如:

计算机中实现:


Matrix3X1 translate3DByAddition(Matrix3X1 start, Matrix3X1 trans)
{
      Matrix3X1 temp;
      temp 
= addMatrices(start,trans);
      
return temp;
}

 

2D乘法平移:

例如:

计算机中的实现:


Matrix3X1 translate2DByMultiplication(Matrix3X1 start,float dx, float dy)
{
         Matrix3X3 temp;
         Matrix3X1 result;

         
//Zero out the matrix.
         temp = createFixed3X3Matrix(0);

         
//setup the 3x3 for multiplication;
         temp.index[0][0= 1;
         temp.index[
1][1= 1;
         temp.index[
2][2= 1;

         
//put in the translation amount
         temp.index[0][2= dx;
         temp.index[
1][2= dy;

         result 
= multiplyMatrixNxM(temp,start);
         
return result;
}

 

3D乘法平移:

例如:

计算机中的实现:


Matrix4X1 translate3DByMultiply(Matrix4X1 start,float dx, float dy,float dz)
{

         Matrix4X4 temp;
         Matrix4X1 result;

         
//Zero out the matrix.
         temp = createFixed4X4Matrix(0);

         
//setup the 4X4 for multiplication;
         temp.index[0][0= 1;
         temp.index[
1][1= 1;
         temp.index[
2][2= 1;
         temp.index[
3][3= 1;

         
//put in the translation amount
         temp.index[0][3= dx;
         temp.index[
1][3= dy;
         temp.index[
2][3= dz;

         result 
= multiplyMatrixNxM(temp,start);
         
return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值