MarteEngine tutorial:Basic collision
在完成Keyboard and mouse input后,现在是时候让两个entities交互了。
在屏幕上有一些基本的东西,你可以移动你的英雄的图像... 但是当两个entities互相接触时会发生什么?这被称为碰撞,MarteEngine能使得它处理起来比你想象的更容易。
让我们从创建两个Entities:Player和Wall 开始:
/**
* @param x, x coordinate on screen where Player starts
* @param y, y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define basic collision info
// hitbox is a rectangle around entity, in this case it is exactly the size of the player image
setHitBox(0, 0, img.getWidth(), img.getHeight());
// declare type of this entity
addType("PLAYER");
}
}
public Wall(float x, float y) {
super(x, y);
// load Image from disk and associate it as wall image
Image img = ResourceManager.getImage("wall");
setGraphic(img);
// define basic collision info
// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
setHitBox(0, 0, img.getWidth(), img.getHeight());
// declare type of this entity
addType(SOLID);
}
}
我们加入的那几行代码有什么用呢?对于每个entity如果想让它对碰撞有所反应的话,你需要声明两件事情:
1.hitbox: hitbox指定一个entity的边界。当MarteEngine检查碰撞时,它查看每个entity的边界是否与其他entity的hitbox重叠。
2.type: 为加速不同entity组的碰撞检测,你可以定义entity的类型。每个entity可以有多种类型。在我们的例子中,Player 属于“PLAYER”类型,而Wall属于内置的基本类型“SOLID”.
有了这两个基本概念,你可以做你想做的任何事,例如在玩家更新它的位置时使用碰撞检测方法,是有可能检查和避免与其他有内置"SOLID"类型entities发生碰撞的。
/**
* @param x
* , x coordinate on screen where Player starts
* @param y
* , y coordinate on screen where Player starts
*/
public Player(float x, float y) {
super(x, y);
// load Image from disk and associate it as player image
Image img = ResourceManager.getImage("player");
setGraphic(img);
// define basic collision info
// hitbox is a rectangle around entity, in this case it is exactly the size of the
// player image
setHitBox(0, 0, img.getWidth(), img.getHeight());
// declare type of this entity
addType("PLAYER");
}
@Override
public void update(GameContainer container, int delta)
throws SlickException {
super.update(container, delta);
// check if a key is down
if (check("RIGHT")) {
// do anything you like, for example: only move right if we don't collide with some SOLID entity
if (collide(SOLID, x + 10, y)==null) {
x = x + 10;
}
}
}
}
在update方法里,在玩家移动之前(按下键盘上的右箭头之后),可能要检查玩家与另一个类型为SOLID的entity是否发生碰撞(在我们的例子中是Wall),如果没有发生碰撞,则玩家可以移动。
你也可以使用工具方法(“callback”) 以墙的视角来处理碰撞:
public Wall(float x, float y) {
super(x, y);
// load Image from disk and associate it as wall image
Image img = ResourceManager.getImage("wall");
setGraphic(img);
// define basic collision info
// hitbox is a rectangle around entity, in this case it is exactly the size of the wall image
setHitBox(0, 0, img.getWidth(), img.getHeight());
// declare type of this entity
addType(SOLID);
}
@Override
public void collisionResponse(Entity other) {
// called when colliding with another entity
}
}
当玩家与墙发生碰撞时,墙应该通过覆写基类Entity中的collisionReponse方法执行一些代码,例如墙可能要下降20个点——假设玩家是一个笨重的人。
现在准备开始Animate sprite教程。
MarteEngine tutorial:Basic collision相关推荐
- 全网史上最全的AR学习开发资源汇总(转载他人整理作品,供参考)
因公司想做一款AR产品,之前没有怎么去接触AR知识,通过这几天的了解,查看文档,终于对AR的技术知识.技术支持有一个大概了解.整理了一份有关AR方向参考文档,有兴趣的可以学习学习. 转载于知乎:AR新 ...
- Open3D DbScanClustering聚类算法
DBSCAN聚类算法,是基于密度的聚类算法.该算法需要两个参数. labels = np.array(pcd.cluster_dbscan(eps=0.02, min_points=10, print ...
- 候选翻译文章列表[示范]
本翻译文章列表持续更新中,大家有好的文章记得告诉我,我会把它们放入该列表的. 个人对于好文章的一些理解: 讲的透彻,不一定要有多少代码,但能够把问题讲清楚 不一定要涉及很难的技术,对于一些基础的东西我 ...
- [译]使用scikit-learn进行机器学习的简介(教程1)
原文:http://www.cnblogs.com/taceywong/p/4568806.html 原文地址:http://scikit-learn.org/stable/tutorial/basi ...
- shaderlab学习一
shaderlab学习一 转载:http://wuchengwu5.blog.163.com/blog/static/852237120111115104310682/ Unity中的材质着色器被称为 ...
- sklearn学习(一)
学习网站 http://scikit-learn.org/stable/tutorial/basic/tutorial.html#machine-learning-the-problem-settin ...
- 【学习笔记】吴恩达机器学习 WEEK2 线性回归 Octave教程
Multivariate Linear Regression Multiple Features Xj(i)X_j^{(i)}Xj(i) 其中j表示迭代次数,i表示矩阵索引 转换 原来:hθ(x)= ...
- linux ba 模拟,在你的 Python 游戏中模拟引力 | Linux 中国
学习如何使用 Python 的 Pygame 模块编程电脑游戏,并开始操作引力. -- Seth Kenlon 真实的世界充满了运动和生活.物理学使得真实的生活如此忙碌和动态.物理学是物质在空间中运动 ...
- 转译和编译_10个有趣又能编译为JavaScript的语言,你用过哪些?
点击上方"IT平头哥联盟",选择"置顶或者星标" 你的关注意义重大! 来源:https://www.sitepoint.com/ 现代应用相比普通的网页有不同的 ...
最新文章
- html建立复选框,创建一个像html复选框一样的div
- Matlab调用函数实现CIC滤波器
- git在已忽略文件夹中不忽略指定文件
- 使用timeit测试Python函数的性能
- c# xml的增删改查操作 xmlDocument 的用法
- 存储图片到第三方云服务器
- 让解析器可以快速处理词法单元之间的空格
- 半监督学习在金融文本分类上的探索和实践
- 使用JRebel进行Java Web项目的热部署
- 素数问题c语言程序,判断素数问题(C语言实现)
- Java程序设计基础笔记 • 【第1章 初识Java】
- Android读取服务器图片
- 用python画一个蜡笔小新
- html5文本框里插图片文字,word应用教程:在文本框内插入图片
- 生物信息学在感染和疫苗研究中的应用
- BG22蓝牙——第三弹 蓝牙的一些入门知识,整理了大佬们的文章和链接
- 另眼看iPad发布。
- Linux之网络设置
- python定义复数数组_python数组
- 数字转化为16进制数