SDL Game Development-第四章(1)-移动

[复制链接]

该用户从未签到

2380

主题

2433

帖子

9139

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
9139
QQ
跳转到指定楼层
楼主
发表于 2017-12-19 09:17:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

想要查看内容赶紧注册登陆吧!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
1.卡笛尔坐标
2.2D向量
3.创建变量控制对象的移动
4.设置一个简单移动系统
5.设置一个输入处理
6.建立一个固定帧率

一、建立一个向量vector2D类
//构造,取得长度,重载+, -, *, /等操作
//正常化向量。v1 * 1 / len
  1. #ifndef __VECTOR_2D__
  2.     #define __VECTOR_2D__

  3. #include <math.h>

  4. class Vector2D
  5. {
  6. public:
  7.     Vector2D(float x, float y) : m_x(x), m_y(y) {}
  8.     float getX() { return m_x; }
  9.     float getY() { return m_y; }
  10.     void setX(float x) { m_x = x; }
  11.     void setY(float y) { m_y = y; }

  12.     float length() { return sqrt(m_x * m_x + m_y * m_y); }
  13.     //重载+运算符,使用:Vector2D v3 = v1 + v2;
  14.     Vector2D operator+(const Vector2D& v2) const { return Vector2D(m_x + v2.m_x, m_y + v2.m_y); }
  15.     //v1 += v2;
  16.     friend Vector2D& operator+=(Vector2D& v1, const Vector2D& v2)
  17.     {
  18.         v1.m_x += v2.m_x;
  19.         v1.m_y += v2.m_y;
  20.         return v1;
  21.     }
  22.     Vector2D operator-(const Vector2D& v2) const { return Vector2D(m_x - v2.m_x, m_y - v2.m_y); }
  23.     friend Vector2D& operator-=(Vector2D& v1, const Vector2D& v2)
  24.     {
  25.         v1.m_x -= v2.m_x;
  26.         v1.m_y -= v2.m_y;
  27.         return v1;
  28.     }
  29.     Vector2D operator*(float scalar) { return Vector2D(m_x * scalar, m_y * scalar); }
  30.     Vector2D& operator*=(float scalar)
  31.     {
  32.         m_x *= scalar;
  33.         m_y *= scalar;
  34.         return *this;
  35.     }
  36.     Vector2D operator/(float scalar) { return Vector2D(m_x / scalar, m_y / scalar); }
  37.     Vector2D& operator/=(float scalar)
  38.     {
  39.         m_x /= scalar;
  40.         m_y /= scalar;
  41.         return *this;
  42.     }
  43.     //正常化
  44.     void normalize()
  45.     {
  46.         float len = length();
  47.         if( len > 0)  //避免除0
  48.             (*this) *= 1 / len;
  49.     }

  50. private:
  51.     float m_x;
  52.     float m_y;
  53. };

  54. #endif // __VECTOR_2D__
复制代码
二、在SDLGameObject类中使用它
1.//SDLGameObject.h
#include "Vector2D.h"
2.删除m_x和m_y变量,使用Vector2D变量:
Vector2D m_position;
3.SDLGameObject.cpp中,修改构造
4.修改draw()中使用坐标的代码
5.最后是具体的Player或Enemy类中的update

三、实现速度控制
位置+速度 = (X位置+X速度, Y位置+Y速度)
1.在SDLGameObject的update()中定义一个速度变量,这样所有派生的类都得到更新
私有成员中添加速度变量定义
2.构造函数变量列表中添加它,并初始化为0, 0
3.在update()中修改它
4.在Player.cpp中修改update(),为速度设置1,并调用父类的update方法

四、添加加速
1.SDLGameObject.h中添加加速变量
  1.      //int m_x;
  2.      //int m_y;
  3.      Vector2D m_position;   //位置
  4.      Vector2D m_velocity;   //速度
  5.      Vector2D m_acceleration;   //加速
复制代码
2.构造函数变量表中添加它
  1. SDLGameObject :: SDLGameObject(const LoaderParams* pParams)
  2. : GameObject(pParams), m_position(pParams->getX(), pParams->getY())
  3. , m_velocity(0, 0), m_acceleration(0, 0)
复制代码
3.SDLGameObject::update中更新
    m_velocity += m_acceleration;
    //m_position += m_velocity;
4.Player::update中更新
  1.      m_currentFrame = int(((SDL_GetTicks()/100) % 6));
  2.      //m_velocity.setX(1);    //每次调用时设置速度为1
  3.      m_acceleration.setX(1);    //加速变量X设1
  4.      SDLGameObject::update();   //调用父类的更新方法,位置中的X被加1
复制代码
五、创建每秒固定帧数
1.在main.cpp中创建常量
//60FPS的选择,是因为这是现在多数屏幕的刷新率
const int FPS = 60;     //固定帧率的常量
const int DELAY_TIME = 1000.0f / FPS;
int main(int argc, char* argv[])
2.定义变量
Uint32 frameStart, frameTime;   //开始和当前时间
3.主循环中使用这些变量
  1.         while(TheGame::Instance()->running())    //开始主循环
  2.         {
  3.             frameStart = SDL_GetTicks();

  4.             TheGame::Instance()->handleEvents(); //处理输入
  5.             TheGame::Instance()->update();   //计算时间和碰撞
  6.             TheGame::Instance()->render();   //渲染到屏幕

  7.             frameTime = SDL_GetTicks() - frameStart;

  8.             if(frameTime < (Uint32) DELAY_TIME)
  9.             {
  10.                 SDL_Delay((int)(DELAY_TIME - frameTime));
  11.                 //延时固定帧率
  12.             }
  13.         }
复制代码


分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

快速回复高级模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表