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. Regular Expression Matching & Wildcard Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...

  2. shell-拷贝指定目录外其他全部目录

    shopt -s extglob if [ ! -d "desdir" ]; then mkdir desdir fi cp -r Oozie/!(.svn*) desdir/

  3. git学习——Git 基础要点【转】

    转自:http://blog.csdn.net/zeroboundary/article/details/10549555 简单地说,Git 究竟是怎样的一个系统呢?请注意,接下来的内容非常重要,若是 ...

  4. ajax与302响应

    在ajax请求中,如果服务器端的响应是302 Found,在ajax的回调函数中能够获取这个状态码吗?能够从Response Headers中得到Location的值进行重定向吗?让我们来一起看看实际 ...

  5. springboot:mybatis多数据源配置

    1.application.properties #CMS数据源(主库) spring.datasource.cms.driver-class-name=com.mysql.jdbc.Driver s ...

  6. Cover Points

    CF#511 div2 B 现场掉分赛(翻车)...qwq 题目大意: 给定n个点的左边,你需要求出一个腰长最短的等腰直角三角形(顶点为坐标轴原点),是的所有点都在这个等腰直角三角形的内部或者边上. ...

  7. Python3.6安装OpenCV

    1.安装依赖 pip install --upgrade setuptools pip install numpy Matplotlib -i https://mirrors.aliyun.com/p ...

  8. 生成和打上patch的方法(转载)

    原文链接:http://my.oschina.net/fgq611/blog/180750 在团队开发的过程中,经常需要生成patch,或者打上别人提供的patch,那么一般情况是如何操作的呢. 首先 ...

  9. MyEclipse如何查找指定工程下所有或指定文件中特定字符串并且可进行批量替换

    查找操作步骤:(1)在myEclipse里菜单选择-Search-Search(快捷键:ctrl+h);(2)在弹出对话框中选File Search选项,然后在第一个文本框中输入“要查找的字符串”(为 ...

  10. IE去掉链接虚线框的几个方法

    虚线框简直就是个多余的东西,上一篇教大家怎么去除Firefox中链接和按钮虚线框,今天叫大家去掉去除IE中链接的虚线框.方法一:利用javascript的onfocus事件,实现如下:Html代码 & ...