Programming 2D Games 读书笔记(第六章)
http://www.programming2dgames.com/chapter6.htm
示例一:Bounce
边界碰撞测试
velocity为移动的速度,
- 超过右边界,velocity.x为负,spriteData.x位置减去宽度
- 超过左边界,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 读书笔记(第六章)的更多相关文章
- Programming 2D Games 读书笔记(第四章)
示例一:Game Engine Part 1 更加完善游戏的基本流程 Graphics添加了以下几个方法,beginScene和endScene提高绘图,showBackbuffer去掉了clea ...
- Programming 2D Games 读书笔记(第五章)
http://www.programming2dgames.com/chapter5.htm 示例一:Planet 真正示例的开始,首先是载入2张图片 1.Graphics添加了2个方法 load ...
- Programming 2D Games 读书笔记(第三章)
示例一:DirectX Window Graphics类用于初始化Direct 3D 主流程: 仅需要粗体部分 try{ // Create Graphics object graphics = ...
- Programming 2D Games 读书笔记(第二章)
本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...
- 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)
<Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...
- C primer plus 读书笔记第六章和第七章
这两章的标题是C控制语句:循环以及C控制语句:分支和跳转.之所以一起讲,是因为这两章内容都是讲控制语句. 第六章的第一段示例代码 /* summing.c --对用户输入的整数求和 */ #inclu ...
- 《R语言实战》读书笔记-- 第六章 基本图形
首先写第二部分的前言. 第二部分用来介绍获取数据基本信息的图形技术和统计方法. 本章主要内容 条形图.箱型图.点图 饼图和扇形图 直方图和核密度图 分析数据第一步就是要观察它,用可视化的方式是最好的. ...
- 《Python基础教程》 读书笔记 第六章 抽象 函数 参数
6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...
- 《利用python进行数据分析》读书笔记--第六章 数据加载、存储与文件格式
http://www.cnblogs.com/batteryhp/p/5021858.html 输入输出一般分为下面几类:读取文本文件和其他更高效的磁盘存储格式,加载数据库中的数据.利用Web API ...
随机推荐
- 基于Window10搭建android开发环境
一.安装JDK 1.下载(网页链接) 2.双击安装文件进行安装,安装在合适目录,例如:D:\Java\jdk1.8.0_201与D:\Java\jre1.8.0_201 3.设置环境变量 3.1.JA ...
- 基于FPGA(DDS)的正弦波发生器
记录背景:昨晚快下班时,与同事rk聊起怎么用FPGA实现正弦波的输出.我第一反应是利用高频的PWM波去滤波,但感觉这样的波形精度肯定很差:后来想起之前由看过怎么用FPGA产生正弦波的技术,但怎么都想不 ...
- centos:SSH登录时间很慢
vi /etc/ssh/sshd_config GSSAPIAuthentication 改为 no 开启UseDNS,值改为 no service sshd restart
- css实现导航切换
css实现导航切换 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <title>css实现导航切换&l ...
- python tar.gz格式压缩、解压
一.压缩 需求描述 现在有一个目录,需要将此目录打包成tar.gz文件.因为有一个Django项目,需要用到此功能! tar.gz 目录结构如下: ./ ├── folder │ ├── .doc ...
- js 相对路径转为绝对路径
有时为了唯一标识网址或其它开发需要,我们需要将相对的网址转换为绝对的网址.当然前人实现方式已经不少,但或多或少的存在缺点或兼容问题.下面我将总结已有实现并给出相对完美的实现. 常规实现:地址转换 因该 ...
- 什么是Less、typescript与webpack?
前端常用技术概述--Less.typescript与webpack 前言:讲起前端,我们就不能不讲CSS与Javascript,在这两种技术广泛应用的今天,他们的扩展也是层出不穷,css的扩展有Les ...
- UI 框架
Vue.js 之 iView UI 框架 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引用一下,不需要就删除.故观 ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图匹配
因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #de ...
- shared_ptr(作为局部变量返回)
智能指针:shared_ptr 1.一个局部的shared_ptr 作为返回值过程:当shared_ptr 被创建的时候,自身的引用计数 +1,当前引用计数为 1 , 按值返回以后 引用计数 + 1 ...