http://www.programming2dgames.com/chapter6.htm

示例一:Bounce

边界碰撞测试

velocity为移动的速度,

  1. 超过右边界,velocity.x为负,spriteData.x位置减去宽度
  2. 超过左边界,velocity.x为正

上下边界同理

//=============================================================================
// update
// typically called once per frame
// frameTime is used to regulate the speed of movement and animation
//=============================================================================
void Ship::update(float frameTime)
{
Entity::update(frameTime);
spriteData.angle += frameTime * shipNS::ROTATION_RATE; // rotate the ship
spriteData.x += frameTime * velocity.x; // move ship along X
spriteData.y += frameTime * velocity.y; // move ship along Y float fScale=getScale();
// Bounce off walls
// if hit right screen edge
if (spriteData.x > GAME_WIDTH-shipNS::WIDTH*fScale)
{
// position at right screen edge
spriteData.x = GAME_WIDTH-shipNS::WIDTH*fScale;
velocity.x = -velocity.x; // reverse X direction
}
else if (spriteData.x < 0) // else if hit left screen edge
{
spriteData.x = 0; // position at left screen edge
velocity.x = -velocity.x; // reverse X direction
}
// if hit bottom screen edge
if (spriteData.y > GAME_HEIGHT-shipNS::HEIGHT*fScale)
{
// position at bottom screen edge
spriteData.y = GAME_HEIGHT-shipNS::HEIGHT*fScale;
velocity.y = -velocity.y; // reverse Y direction
}
else if (spriteData.y < 0) // else if hit top screen edge
{
spriteData.y = 0; // position at top screen edge
velocity.y = -velocity.y; // reverse Y direction
}
}

示例二:Planet Collision

参考:http://wenku.baidu.com/view/45544cfcfab069dc50220145.html

1.包围球碰撞

//=============================================================================
// Circular collision detection method
// Called by collision(), default collision detection method
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideCircle(Entity &ent, VECTOR2 &collisionVector)
{
// difference between centers
distSquared = *getCenter() - *ent.getCenter();
distSquared.x = distSquared.x * distSquared.x; // difference squared
distSquared.y = distSquared.y * distSquared.y; // Calculate the sum of the radii (adjusted for scale)
sumRadiiSquared = (radius*getScale()) + (ent.radius*ent.getScale());
sumRadiiSquared *= sumRadiiSquared; // square it // if entities are colliding
if(distSquared.x + distSquared.y <= sumRadiiSquared)
{
// set collision vector
collisionVector = *ent.getCenter() - *getCenter();
return true;
}
return false; // not colliding
}

2.AABB碰撞检测

//=============================================================================
// Axis aligned bounding box collision detection method
// Called by collision()
// Post: returns true if collision, false otherwise
// sets collisionVector if collision
//=============================================================================
bool Entity::collideBox(Entity &ent, VECTOR2 &collisionVector)
{
// if either entity is not active then no collision may occcur
if (!active || !ent.getActive())
return false; // Check for collision using Axis Aligned Bounding Box.
if( (getCenterX() + edge.right*getScale() >= ent.getCenterX() + ent.getEdge().left*ent.getScale()) &&
(getCenterX() + edge.left*getScale() <= ent.getCenterX() + ent.getEdge().right*ent.getScale()) &&
(getCenterY() + edge.bottom*getScale() >= ent.getCenterY() + ent.getEdge().top*ent.getScale()) &&
(getCenterY() + edge.top*getScale() <= ent.getCenterY() + ent.getEdge().bottom*ent.getScale()) )
{
// set collision vector
collisionVector = *ent.getCenter() - *getCenter();
return true;
} return false;
}

Programming 2D Games 读书笔记(第六章)的更多相关文章

  1. Programming 2D Games 读书笔记(第四章)

      示例一:Game Engine Part 1 更加完善游戏的基本流程 Graphics添加了以下几个方法,beginScene和endScene提高绘图,showBackbuffer去掉了clea ...

  2. Programming 2D Games 读书笔记(第五章)

      http://www.programming2dgames.com/chapter5.htm 示例一:Planet 真正示例的开始,首先是载入2张图片 1.Graphics添加了2个方法 load ...

  3. Programming 2D Games 读书笔记(第三章)

      示例一:DirectX Window Graphics类用于初始化Direct 3D 主流程: 仅需要粗体部分 try{ // Create Graphics object graphics = ...

  4. Programming 2D Games 读书笔记(第二章)

      本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...

  5. 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)

    <Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...

  6. C primer plus 读书笔记第六章和第七章

    这两章的标题是C控制语句:循环以及C控制语句:分支和跳转.之所以一起讲,是因为这两章内容都是讲控制语句. 第六章的第一段示例代码 /* summing.c --对用户输入的整数求和 */ #inclu ...

  7. 《R语言实战》读书笔记-- 第六章 基本图形

    首先写第二部分的前言. 第二部分用来介绍获取数据基本信息的图形技术和统计方法. 本章主要内容 条形图.箱型图.点图 饼图和扇形图 直方图和核密度图 分析数据第一步就是要观察它,用可视化的方式是最好的. ...

  8. 《Python基础教程》 读书笔记 第六章 抽象 函数 参数

    6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...

  9. 《利用python进行数据分析》读书笔记--第六章 数据加载、存储与文件格式

    http://www.cnblogs.com/batteryhp/p/5021858.html 输入输出一般分为下面几类:读取文本文件和其他更高效的磁盘存储格式,加载数据库中的数据.利用Web API ...

随机推荐

  1. CentOS配置源

    一.源列表 aliyun源 #各系统版本repo文件对应的下载操作 CentOS wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.al ...

  2. [Alg::Trick]小白鼠找毒酒

    题目来源:牛客网 https://www.nowcoder.com/questionTerminal/c26c4e43c77440ee9497b20118871bf1 8瓶酒一瓶有毒,用人测试.每次测 ...

  3. echo变量失败,提示:ECHO 处于关闭状态

    检查变量值,变量值为空就会提示关闭

  4. 修改MySQL的时区,涉及参数time_zone

    原地址:http://blog.csdn.net/mchdba/article/details/9763521 首先需要查看mysql的当前时区,用time_zone参数 mysql> show ...

  5. ICCV2013 录用论文(目标跟踪相关部分)(转)

    单目标(表观模型): 1. Seunghoon Hong, BohyungHan. Orderless Trackingthrough Model-Averaged Density Estimatio ...

  6. 【linux】环境变量

    参考链接: http://www.cnblogs.com/growup/archive/2011/07/02/2096142.html https://zhidao.baidu.com/questio ...

  7. laravel 网站地图轮子

    https://github.com/Laravelium/laravel-sitemap add the following to your composer.json file : For Lar ...

  8. Fiddler 常用功能总结

    1.fiddler相关配置   2.如何抓包 移动端 ①保持手机和电脑处于同一网络中 ②设置手机的代理为电脑当前所处网络的IP,端口号为:8888,eg:10.12.1.64:8888. ③ 启动ap ...

  9. SQL Server 3

    一.数据压缩 1.行压缩 行压缩可将固定长度类型存储为可变长度存储类型.例如,使用char(100)数据列存储字符串“SQL Server 2012”,压缩后只需要存放15个字符.(这种压缩模式,将对 ...

  10. 操作数组不要只会for循环

    很多时候,我们在操作数组的时候往往就是一个for循环干到底,对数组提供的其它方法视而不见.看完本文,希望你可以换种思路处理数组,然后可以写出更加漂亮.简洁.函数式的代码. reduce 数组里所有值的 ...