博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四叉树的js实现
阅读量:5349 次
发布时间:2019-06-15

本文共 1555 字,大约阅读时间需要 5 分钟。

基于

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:

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:  and .

  • 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  available that allows you to update and remove single objects. This can be handy when most of the objects in your Quadtree are static.

转载于:https://www.cnblogs.com/vana/p/10950435.html

你可能感兴趣的文章
Nodejs Express模块server.address().address为::
查看>>
4.3.5 Sticks (POJ1011)
查看>>
POJ 2960 S-Nim 博弈论 sg函数
查看>>
Dijkstra模版
查看>>
一个简单的插件式后台任务管理程序
查看>>
GDB调试多进程程序
查看>>
组合数
查看>>
CMD批处理延时启动的几个方法
查看>>
转:LoadRunner中web_custom_request 和 web_submit_data的差别
查看>>
HTC G7直刷MIUI开启A2SD+亲测教程
查看>>
shiro的rememberMe不生效
查看>>
const 不兼容的类型限定符问题
查看>>
OpenCV的配置
查看>>
spring Cache + Redis 开发数据字典以及自定义标签
查看>>
成功连上数据库顿感世界美好许多
查看>>
编程注意2
查看>>
《C++ Primer Plus》第12章 类和动态内存分配 学习笔记
查看>>
javascript中sort()排序方法总结
查看>>
实现聊天界面的代码
查看>>
自己生成一个NDK的浅析
查看>>