Hi every coppercube lovers,
I asked a question about snap to grid the objects during the game like sea battle games and nikholaus send me that script.
I says lots of thanks for share this script.
I'm sharing here for maybe it helps someone.
function snap(value, gridSize, roundFunction){ if (roundFunction === undefined) roundFunction = Math.round; return gridSize * roundFunction(value / gridSize); }
var global_gridSnapValue = 5;
function snapNodeToGrid(s) { var position = ccbGetSceneNodeProperty(s, "Position");
var moveobjX = snap(position.x, global_gridSnapValue); var moveobjY = snap(position.y, global_gridSnapValue); var moveobjZ = snap(position.z, global_gridSnapValue);
ccbSetSceneNodeProperty(s,"Position",moveobjX,moveobjY,moveobjZ); ccbUpdateSceneNodeBoundingBox(s); }
---------------------------------------------------------
Then, to snap a node with for example the name 'ship' to grid, do it like that:
snapNodeToGrid(ccbGetSceneNodeFromName("ship"));
|