Skip to content

Commit 7eaa456

Browse files
committed
Simple camera culling
1 parent ff86440 commit 7eaa456

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

v3/src/camera/Camera.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var Camera = new Class({
2626
this.zoom = 1.0;
2727
this.rotation = 0.0;
2828
this.matrix = new TransformMatrix(1, 0, 0, 1, 0, 0);
29+
this.culledObjects = [];
2930

3031
// shake
3132
this._shakeDuration = 0.0;
@@ -55,6 +56,37 @@ var Camera = new Class({
5556
this.transparent = true;
5657
},
5758

59+
cull: function (renderableObjects)
60+
{
61+
var scrollX = this.scrollX;
62+
var scrollY = this.scrollY;
63+
var cameraW = this.width;
64+
var cameraH = this.height;
65+
var culledObjects = this.culledObjects;
66+
var length = renderableObjects.length;
67+
68+
culledObjects.length = 0;
69+
70+
for (var index = 0; index < length; ++index)
71+
{
72+
var object = renderableObjects[index];
73+
var objectW = object.width;
74+
var objectH = object.height;
75+
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
76+
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
77+
var cullW = cameraW + objectW;
78+
var cullH = cameraH + objectH;
79+
80+
if (objectX > -objectW && objectY > -objectH &&
81+
objectX < cullW && objectY < cullH)
82+
{
83+
culledObjects.push(object);
84+
}
85+
}
86+
87+
return culledObjects;
88+
},
89+
5890
setBackgroundColor: function (color)
5991
{
6092
if (color === undefined) { color = 'rgba(0,0,0,0)'; }
@@ -280,8 +312,8 @@ var Camera = new Class({
280312
pointOut = {x: 0, y: 0};
281313
}
282314

283-
pointOut.x = (x * mva + y * mvc + mve) - scrollX;
284-
pointOut.y = (x * mvb + y * mvd + mvf) - scrollY;
315+
pointOut.x = (x * mva + y * mvc + mve);
316+
pointOut.y = (x * mvb + y * mvd + mvf);
285317

286318
return pointOut;
287319
},
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var IntersectGameObject = function (point, gameObjectArray, camera)
2+
{
3+
return false;
4+
};
5+
6+
module.exports = IntersectGameObject;

v3/src/gameobjects/tilemap/dynamic/Tilemap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ var Tilemap = new Class({
128128
var cullH = cameraH + tileH;
129129

130130
if (tile.visible &&
131-
tileX > -tileH && tileY > -tileW &&
131+
tileX > -tileW && tileY > -tileH &&
132132
tileX < cullW && tileY < cullH)
133133
{
134134
culledTiles.push(tile);

0 commit comments

Comments
 (0)