# Haw to snap a moving to a grid

## Recommended Posts

Hello,

I began to make a Battle City clone for studying a programming in TypeScript and Phaser. You can run it: https://8observer8.bitbucket.io/Phaser/BattleCity/dist/

I don't understand how to write an algorithm for snapping a tank to a grid how it is in an original game. When I turn a tank I need to snap tank coordinates to: 16, 32, 48, 64, ..., 384, 400.

##### Share on other sites

Divide the coordinate by the snap value (16 in your case), then round the number to an integer, and then mutiply by the snap value.

So, for example, you want to snap an x-coordinate of 37 to the nearest multiple of 16:

var x = 37;
var snapValue = 16;
var snappedX = Math.round(x / snapValue) * snapValue;

This will give snappedX as 32.

(Nice game, by the way.  You'll want to tween the tank to the nearest snap value on releasing the cursor key, so it's always in the right position before turning.  And so that you don't get the tank moving backwards on releasing the key, you should change Math.round in the algorithm above to either Math.floor or Math.ceil depending on which direction it's going, so it always tweens forward to the correct position)

##### Share on other sites

Thank you very much for your help!

Now it is a correct moving: https://8observer8.bitbucket.io/Phaser/BattleCity/dist/

There is not TypeScript in "Code snippet"

/// <reference path="./libs/phaser.d.ts" />

export namespace BattleCity
{
enum Direction
{
Up, Left, Down, Right
}

export class Player extends Phaser.Sprite
{

private speed: number = 80;
private currDir = Direction.Up;
private xOffset: number = 32;
private yOffset: number = 16;

constructor(game: Phaser.Game, x: number, y: number)
{
super(game, x, y, "battleCityScene", "tank_yellow_small_up_01.png");
this.anchor.setTo(0.5);
game.physics.enable(this);

// Idle animations

// Move animations
this.animations.add("driveUp", ["tank_yellow_small_up_01.png", "tank_yellow_small_up_02.png"], 10, true, false);
this.animations.add("driveDown", ["tank_yellow_small_down_01.png", "tank_yellow_small_down_02.png"], 10, true, false);
this.animations.add("driveLeft", ["tank_yellow_small_left_01.png", "tank_yellow_small_left_02.png"], 10, true, false);
this.animations.add("driveRight", ["tank_yellow_small_right_01.png", "tank_yellow_small_right_02.png"], 10, true, false);
}

update()
{
this.body.velocity.x = 0;
this.body.velocity.y = 0;

if (this.game.input.keyboard.isDown(Phaser.Keyboard.W) ||
this.game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
this.animations.play("driveUp");

if (this.currDir != Direction.Up &&
this.currDir != Direction.Down)
{
this.x = this.snapToGrid(this.x - this.xOffset) + this.xOffset;
}

this.currDir = Direction.Up;
this.body.velocity.y = -this.speed;
}
else if (this.game.input.keyboard.isDown(Phaser.Keyboard.A) ||
this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
this.animations.play("driveLeft");

if (this.currDir != Direction.Left &&
this.currDir != Direction.Right)
{
this.y = this.snapToGrid(this.y - this.yOffset) + this.yOffset;
}

this.currDir = Direction.Left;
this.body.velocity.x = -this.speed;
}
else if (this.game.input.keyboard.isDown(Phaser.Keyboard.S) ||
this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
this.animations.play("driveDown");

if (this.currDir != Direction.Up &&
this.currDir != Direction.Down)
{
this.x = this.snapToGrid(this.x - this.xOffset) + this.xOffset;
}

this.currDir = Direction.Down;
this.body.velocity.y = this.speed;
}
else if (this.game.input.keyboard.isDown(Phaser.Keyboard.D) ||
this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
this.animations.play("driveRight");

if (this.currDir != Direction.Left &&
this.currDir != Direction.Right)
{
this.y = this.snapToGrid(this.y - this.yOffset) + this.yOffset;
}

this.currDir = Direction.Right;
this.body.velocity.x = this.speed;
}
else
{
this.body.velocity.x = 0;
this.body.velocity.y = 0;
switch (this.currDir)
{
case Direction.Up:
this.animations.play("idleUp");
break;
case Direction.Left:
this.animations.play("idleLeft");
break;
case Direction.Down:
this.animations.play("idleDown");
break;
case Direction.Right:
this.animations.play("idleRight");
break;
}
}
}

/**
* Snap coordinate to a grid
* @param coordinate from (8, 407]
* @return snapped coordinate, like: 16, 32, 48, ..., 384, 400
*/
private snapToGrid(coordinate: number): number
{
let stepValue = 16;

let rangeNumber = coordinate / stepValue;
rangeNumber = rangeNumber - (rangeNumber % 1);
// We get a rangeNumber like: 0, 1, 2, ..., 25

// Snap a coordinate to the nearest grid node
let remainder = coordinate % stepValue;
let snappedCoordinate: number;
if (remainder > stepValue / 2)
{
snappedCoordinate = (rangeNumber + 1) * stepValue;
}
else
{
snappedCoordinate = rangeNumber * stepValue;
}

return snappedCoordinate;
}
}
}

##### Share on other sites

5 hours ago, Andromedus said:

so it always tweens forward to the correct position

It is a very good idea! I will make it.