Skip to content

Commit cecb1d9

Browse files
committed
Add a fat box.
1 parent e859a59 commit cecb1d9

File tree

1 file changed

+124
-34
lines changed

1 file changed

+124
-34
lines changed

examples/rtree/game.json

+124-34
Original file line numberDiff line numberDiff line change
@@ -989,12 +989,19 @@
989989
{
990990
"type": "BuiltinCommonInstructions::JsCode",
991991
"inlineCode": [
992-
"gdjs.__collisionDetectorExtension = gdjs.__collisionDetectorExtension || {};",
992+
"/**",
993+
" * @param {gdjs.BehaviorRBushAABB} fatAABB",
994+
" * @param {gdjs.AABB} aabb",
995+
" */",
996+
"const includesAABB = function(fatAABB, aabb) {",
997+
" return fatAABB.minX <= aabb.min[0] && aabb.max[0] <= fatAABB.maxX",
998+
" && fatAABB.minY <= aabb.min[1] && aabb.max[1] <= fatAABB.maxY;",
999+
"}",
9931000
"",
9941001
"/**",
9951002
" * ",
9961003
" */",
997-
"gdjs.__collisionDetectorExtension.SolidManager = /** @class */ (function () {",
1004+
"const SolidManager = /** @class */ (function () {",
9981005
" /**",
9991006
" * Create a manager.",
10001007
" */",
@@ -1003,30 +1010,53 @@
10031010
" }",
10041011
"",
10051012
" /**",
1006-
" * Add a boid to the R-Tree.",
1013+
" * Add an object to the R-Tree.",
10071014
" * @param {gdjs.RuntimeBehavior} behavior",
10081015
" */",
10091016
" SolidManager.prototype.add = function (behavior) {",
1010-
" if (behavior.currentRBushAABB) {",
1011-
" behavior.currentRBushAABB.updateAABBFromOwner();",
1012-
" }",
1013-
" else {",
1017+
" if (!behavior.currentRBushAABB) {",
10141018
" behavior.currentRBushAABB = new gdjs.BehaviorRBushAABB(",
10151019
" behavior",
10161020
" );",
10171021
" }",
1022+
" //behavior.currentRBushAABB.updateAABBFromOwner();",
1023+
" const aabb = behavior.owner.getAABB();",
1024+
" const fatFactor = behavior._getFatFactor();",
1025+
" const marginX = fatFactor * (aabb.min[0] - aabb.max[0]);",
1026+
" const marginY = fatFactor * (aabb.min[1] - aabb.max[1]);",
1027+
" const fatAABB = behavior.currentRBushAABB;",
1028+
" fatAABB.minX = aabb.min[0] - marginX;",
1029+
" fatAABB.minY = aabb.min[1] - marginY;",
1030+
" fatAABB.maxX = aabb.max[0] + marginX;",
1031+
" fatAABB.maxY = aabb.max[1] + marginY;",
1032+
" ",
10181033
" const rBush = this.getRBush(behavior.owner.getName());",
1019-
" rBush.insert(behavior.currentRBushAABB);",
1034+
" rBush.insert(fatAABB);",
10201035
" }",
10211036
"",
10221037
" /**",
1023-
" * Remove a boid from the R-Tree.",
1038+
" * Remove an object from the R-Tree.",
10241039
" * @param {gdjs.RuntimeBehavior} behavior",
10251040
" */",
10261041
" SolidManager.prototype.remove = function (behavior) {",
10271042
" const rBush = this.getRBush(behavior.owner.getName());",
10281043
" rBush.remove(behavior.currentRBushAABB);",
10291044
" }",
1045+
" ",
1046+
" /**",
1047+
" * Update an object to the R-Tree.",
1048+
" * @param {gdjs.RuntimeBehavior} behavior",
1049+
" */",
1050+
" SolidManager.prototype.update = function (behavior) {",
1051+
" const fatAABB = behavior.currentRBushAABB;",
1052+
" const aabb = behavior.owner.getAABB();",
1053+
"",
1054+
" if (!includesAABB(fatAABB, aabb)) {",
1055+
" const rBush = this.getRBush(behavior.owner.getName());",
1056+
" this.remove(behavior);",
1057+
" this.add(behavior);",
1058+
" }",
1059+
" }",
10301060
"",
10311061
" SolidManager.prototype.getRBush = function (objectName) {",
10321062
" let rBush = this.rBushes.get(objectName);",
@@ -1049,7 +1079,7 @@
10491079
" */",
10501080
" SolidManager.prototype.getAllInstancesInRectangle = function (objectName, left, top, right, bottom, results) {",
10511081
" const searchArea = gdjs.staticObject(",
1052-
" gdjs.__collisionDetectorExtension.SolidManager.prototype.getAllInstancesInRectangle",
1082+
" SolidManager.prototype.getAllInstancesInRectangle",
10531083
" );",
10541084
" searchArea.minX = left;",
10551085
" searchArea.minY = top;",
@@ -1065,6 +1095,9 @@
10651095
"",
10661096
" return SolidManager;",
10671097
"}());",
1098+
"",
1099+
"gdjs.__collisionDetectorExtension = gdjs.__collisionDetectorExtension || {};",
1100+
"gdjs.__collisionDetectorExtension.SolidManager = SolidManager;",
10681101
""
10691102
],
10701103
"parameterObjects": "",
@@ -1284,6 +1317,35 @@
12841317
"useStrict": true,
12851318
"eventsSheetExpanded": false
12861319
},
1320+
{
1321+
"type": "BuiltinCommonInstructions::Standard",
1322+
"conditions": [
1323+
{
1324+
"type": {
1325+
"value": "CollisionDetector::Solid::PropertySolidType"
1326+
},
1327+
"parameters": [
1328+
"Object",
1329+
"Behavior",
1330+
"=",
1331+
"\"Static\""
1332+
]
1333+
}
1334+
],
1335+
"actions": [
1336+
{
1337+
"type": {
1338+
"value": "CollisionDetector::Solid::SetPropertyFatFactor"
1339+
},
1340+
"parameters": [
1341+
"Object",
1342+
"Behavior",
1343+
"=",
1344+
"0"
1345+
]
1346+
}
1347+
]
1348+
},
12871349
{
12881350
"type": "BuiltinCommonInstructions::Standard",
12891351
"conditions": [],
@@ -1437,6 +1499,23 @@
14371499
"sentence": "",
14381500
"events": [
14391501
{
1502+
"type": "BuiltinCommonInstructions::Standard",
1503+
"conditions": [],
1504+
"actions": [
1505+
{
1506+
"type": {
1507+
"value": "CollisionDetector::Solid::UpdateInTree"
1508+
},
1509+
"parameters": [
1510+
"Object",
1511+
"Behavior",
1512+
""
1513+
]
1514+
}
1515+
]
1516+
},
1517+
{
1518+
"disabled": true,
14401519
"type": "BuiltinCommonInstructions::Standard",
14411520
"conditions": [
14421521
{
@@ -1596,30 +1675,17 @@
15961675
"sentence": "Update _PARAM0_ in the tree",
15971676
"events": [
15981677
{
1599-
"type": "BuiltinCommonInstructions::Standard",
1600-
"conditions": [],
1601-
"actions": [
1602-
{
1603-
"type": {
1604-
"value": "CollisionDetector::Solid::RemoveFromTree"
1605-
},
1606-
"parameters": [
1607-
"Object",
1608-
"Behavior",
1609-
""
1610-
]
1611-
},
1612-
{
1613-
"type": {
1614-
"value": "CollisionDetector::Solid::AddToTree"
1615-
},
1616-
"parameters": [
1617-
"Object",
1618-
"Behavior",
1619-
""
1620-
]
1621-
}
1622-
]
1678+
"type": "BuiltinCommonInstructions::JsCode",
1679+
"inlineCode": [
1680+
"const object = objects[0];\r",
1681+
"const behaviorName = eventsFunctionContext.getBehaviorName(\"Behavior\");\r",
1682+
"const behavior = object.getBehavior(behaviorName);\r",
1683+
"\r",
1684+
"runtimeScene.__collisionDetectorExtension.solidManager.update(behavior);"
1685+
],
1686+
"parameterObjects": "Object",
1687+
"useStrict": true,
1688+
"eventsSheetExpanded": false
16231689
}
16241690
],
16251691
"parameters": [
@@ -1762,6 +1828,30 @@
17621828
"extraInformation": [],
17631829
"hidden": true,
17641830
"name": "PreviousAngle"
1831+
},
1832+
{
1833+
"value": "0.5",
1834+
"type": "Number",
1835+
"label": "",
1836+
"description": "",
1837+
"group": "",
1838+
"extraInformation": [],
1839+
"hidden": true,
1840+
"name": "FatFactor"
1841+
},
1842+
{
1843+
"value": "Automatic",
1844+
"type": "Choice",
1845+
"label": "Type",
1846+
"description": "",
1847+
"group": "",
1848+
"extraInformation": [
1849+
"Automatic",
1850+
"Mobile",
1851+
"Static"
1852+
],
1853+
"hidden": false,
1854+
"name": "SolidType"
17651855
}
17661856
],
17671857
"sharedPropertyDescriptors": []

0 commit comments

Comments
 (0)