Skip to content

Commit

Permalink
Updated rounding to use Math.floor + 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Jul 28, 2024
1 parent 45db4b9 commit a3470a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions src/cameras/2d/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ var Camera = new Class({

if (this.roundPixels)
{
sx = Math.floor(sx);
sy = Math.floor(sy);
sx = Math.floor(sx + 0.5);
sy = Math.floor(sy + 0.5);
}

if (this.useBounds)
Expand All @@ -569,22 +569,22 @@ var Camera = new Class({
this.scrollX = sx;
this.scrollY = sy;

var midX = Math.floor(sx + halfWidth);
var midY = Math.floor(sy + halfHeight);
var midX = Math.floor((sx + halfWidth) + 0.5);
var midY = Math.floor((sy + halfHeight) + 0.5);

// The center of the camera, in world space, so taking zoom into account
// Basically the pixel value of what it's looking at in the middle of the cam
this.midPoint.set(midX, midY);

var displayWidth = Math.floor(width / zoomX);
var displayHeight = Math.floor(height / zoomY);
var displayWidth = Math.floor((width / zoomX) + 0.5);
var displayHeight = Math.floor((height / zoomY) + 0.5);

var vwx = Math.floor(midX - (displayWidth / 2));
var vwy = Math.floor(midY - (displayHeight / 2));
var vwx = Math.floor((midX - (displayWidth / 2)) + 0.5);
var vwy = Math.floor((midY - (displayHeight / 2)) + 0.5);

this.worldView.setTo(vwx, vwy, displayWidth, displayHeight);

matrix.applyITRS(Math.floor(this.x + originX), Math.floor(this.y + originY), this.rotation, zoomX, zoomY);
matrix.applyITRS(Math.floor(this.x + originX + 0.5), Math.floor(this.y + originY + 0.5), this.rotation, zoomX, zoomY);

matrix.translate(-originX, -originY);

Expand Down
20 changes: 10 additions & 10 deletions src/gameobjects/components/TransformMatrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,17 +938,17 @@ var TransformMatrix = new Class({

if (roundPixels)
{
quad[0] = Math.floor(x * a + y * c + e);
quad[1] = Math.floor(x * b + y * d + f);
quad[0] = Math.floor((x * a + y * c + e) + 0.5);
quad[1] = Math.floor((x * b + y * d + f) + 0.5);

quad[2] = Math.floor(x * a + yh * c + e);
quad[3] = Math.floor(x * b + yh * d + f);
quad[2] = Math.floor((x * a + yh * c + e) + 0.5);
quad[3] = Math.floor((x * b + yh * d + f) + 0.5);

quad[4] = Math.floor(xw * a + yh * c + e);
quad[5] = Math.floor(xw * b + yh * d + f);
quad[4] = Math.floor((xw * a + yh * c + e) + 0.5);
quad[5] = Math.floor((xw * b + yh * d + f) + 0.5);

quad[6] = Math.floor(xw * a + y * c + e);
quad[7] = Math.floor(xw * b + y * d + f);
quad[6] = Math.floor((xw * a + y * c + e) + 0.5);
quad[7] = Math.floor((xw * b + y * d + f) + 0.5);
}
else
{
Expand Down Expand Up @@ -1022,7 +1022,7 @@ var TransformMatrix = new Class({

if (round)
{
v = Math.round(v);
v = Math.floor(v + 0.5);
}

return v;
Expand All @@ -1048,7 +1048,7 @@ var TransformMatrix = new Class({

if (round)
{
v = Math.round(v);
v = Math.floor(v + 0.5);
}

return v;
Expand Down

0 comments on commit a3470a0

Please sign in to comment.