Programming 2D Games 读书笔记(第六章)
2013-09-23 19:40 Clingingboy 阅读(316) 评论(0) 编辑 收藏 举报
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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2006-09-23 asp.net控件开发基础(13)