四叉树的js实现
基于
https://gamedevelopment.tutsplus.com/tutorials/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space--gamedev-374
quadtree-js
This is a JavaScript Quadtree implementation of the Java Methods described in this tutorial:http://gamedev.tutsplus.com/tutorials/implementation/quick-tip-use-quadtrees-to-detect-likely-collisions-in-2d-space/
This is not a collision engine, but an algorithm to narrow down objects of possible collision.
Please read the tutorial if you want to know more about Quadtrees.
There are two examples: simple and dynamic.
- red squares represent Quadtree Nodes
- empty white squares represent objects in our Quadtree
- the cursor is the area we constantly test for
- objects turned green are candidates for collision, returned from the receive-function
How to use
Create a new Quadtree with default values for max_objects (10) and max_levels (4)
var myTree = new Quadtree({
x: 0,
y: 0,
width: 400,
height: 300
});
If you want to specify max_objects and max_levels on your own, you can pass them as a 2nd and 3rd argument
var myTree = new Quadtree({
x: 0,
y: 0,
width: 800,
height: 600
}, 5, 8);
Insert an element in the Quadtree
myTree.insert({
x : 200,
y : 150,
width : 20,
height : 20
});
Retrieve elements that "collide" with the given bounds
var elements = myTree.retrieve({
x : 150,
y : 100,
width : 20,
height : 20
});
Clear the Quadtree
myTree.clear();
Check out the examples for more information. Feel free to open an issue if you have any problems.
There is an alternative quadtree-js hitman branch available that allows you to update and remove single objects. This can be handy when most of the objects in your Quadtree are static.
四叉树的js实现的更多相关文章
- js实现四叉树算法
最近在看canvas动画方面教程,里面提到了采用四叉树检测碰撞.之前也看到过四叉树这个名词,但是一直不是很懂.于是就又找了一些四叉树方面的资料看了看,做个笔记,就算日后忘了,也可以回来看看. Quad ...
- Vue.js 和 MVVM 小细节
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
- js学习笔记:操作iframe
iframe可以说是比较老得话题了,而且网上也基本上在说少用iframe,其原因大致为:堵塞页面加载.安全问题.兼容性问题.搜索引擎抓取不到等等,不过相对于这些缺点,iframe的优点更牛,跨域请求. ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- JS调用Android、Ios原生控件
在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...
- jquery和Js的区别和基础操作
jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...
- 利用snowfall.jquery.js实现爱心满屏飞
小颖在上一篇一步一步教你用CSS画爱心中已经分享一种画爱心的方法,这次再分享一种方法用css画爱心,并利用snowfall.jquery.js实现爱心满屏飞的效果. 第一步: 利用伪元素before和 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- JS正则表达式常用总结
正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...
随机推荐
- 通过Playbook部署LAMP
Ansible的PlayBook文件格式为YAML语言,所以希望你在编写PlayBook前对YAML语法有一定的了解,否则在运行PlayBook的时候经常碰到语法错误提示,这里我们通过介绍批量部署LA ...
- Java new运算符解析
1.创建数组时,不使用new操作符 Person [] a; a[0]=new Person(); //Error:variable a might not have been initialized ...
- oa_mvc_easyui_分页(4)
1.数据层的编写 NewListInfoDal.cs: GetPageEntityList方法,根据start,end取出数据 --row_number() over()函数查询 LoadEntity ...
- tasks.json 配置 解决vscode控制台乱码问题
{ "version": "2.0.0", "command": "dotnet", "tasks" ...
- 总结 | 慢 SQL 问题经验总结
1. 导致慢 SQL 的原因 在遇到慢 SQL 情况时,不能简单的把原因归结为 SQL 编写问题(虽然这是最常见的因素),实际上导致慢 SQL 有很多因素,甚至包括硬件和 mysql 本身的 bug. ...
- HDFS NFS Gateway
NFS网关支持NFSv3,并允许将HDFS作为客户端本地文件系统进行挂载.目前,NFS Gateway支持并启用以下使用模式: 用户可以通过NFSv3客户端兼容操作系统上的本地文件系统浏览HDFS文件 ...
- python之requests示例
一) import requests def download(url, num_tries=, user_agent='wswp', proxies=None): ''' 下载指定url并返回网页内 ...
- Reference to ‘xxxxx’ is ambiguous 错误
1.原因,在当前类重复引入了 类库,比如 pch里面导入了#import "XXX" 此类的.h 又引入 #import <xxx/xxx> 导致 解决方法:删除此类的 ...
- automake中Makefile.am和configure.ac的格式及编译过程
step1:写Makefile.am step2:执行autoscan,会生成configurae.scan,修改configure.scan内容之后,命名为configure.ac step3:执行 ...
- jq常用方法
$().addClass(css中定义的样式类型); 给某个元素添加样式$().attr({src:”test.jpg”,alt:”test Image”}); 给 ...