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 ...
随机推荐
- bellman-ford算法(判断有没有负环)
#include <iostream> #include <vector> #include<string> #include<cstring> usi ...
- fuzz for test of the Net::HTTP::GET
use Net::HTTP::GET; % %0e%0f ' *%26 @.jpg>; my $count = 0; for @chars X @chars X @chars X @chars ...
- 第一篇:初始Golang
Golang简介 编程语言已经非常多,偏性能敏感的编译型语言有 C.C++.Java.C#.Delphi和Objective-C 等,偏快速业务开发的动态解析型语言有PHP.Python.Perl.R ...
- casperjs批量执行多个url
var fs=require("fs"); ////-------sample.js-------// //casperオブジェクトを生成var casper = require( ...
- 『实践』Yalmip+Ipopt+Cplex使用手册
Yalmip+Ipopt+Cplex使用手册 1.软件版本 Cplex 12.6.2,Matlab R2014a,Ipopt 3.12.9,Yalmip 2.Cplex添加方法 官方下载地址: htt ...
- 使用SpringCloud搭建高可用服务注册中心
我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭建好的服务注册中心是一个单节点的服务注册中心,这 ...
- python实现广度优先搜索和深度优先搜索
图的概念 图表示的是多点之间的连接关系,由节点和边组成.类型分为有向图,无向图,加权图等,任何问题只要能抽象为图,那么就可以应用相应的图算法. 用字典来表示图 这里我们以有向图举例,有向图的邻居节点是 ...
- MySQL安装与初步操作
MySQL是一款出色的中小型关系数据库,做Java Web开发时,要做到数据持久化存储,选择一款数据库软件自然必不可少. 由于MySQL社区版开元免费,功能比较强大,在此以MySQL为例,演示MySQ ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- H5新增语义化标签
一.根据页面的结构,由div派生的标签. <header> <footer> <nav> 导航 在H4中导航栏一般用ul-li标签,H5中可以直接用<nav& ...