ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Help with CopperCube
forum topic indicator snap to grid in game
person icon
spicymilk
Registered User
Quote
2025-02-12 01:07:18

Is there a script for in game snapping to a gri like say if i was trying to put a box anywhere in a room it coul snap onto a grid

thankyou

person icon
okeoke
Registered User
Quote
2025-02-12 10:50:16

I once made a level editor for my game. Code:
 
this.editorDetectorNode = ccbGetSceneNodeFromName('editor__detector');
this.cellSize = 10;
//...
const detectorPos = ccbGetSceneNodeProperty(this.editorDetectorNode, 'PositionAbs');
const blockPos = new vector3d(
Math.round(detectorPos.x / this.cellSize) * this.cellSize,
Math.round(detectorPos.y / this.cellSize) * this.cellSize,
Math.round(detectorPos.z / this.cellSize) * this.cellSize
);

ccbSetSceneNodeProperty(this.currentBlock, 'Position', blockPos);
Basically, it snaps cubes to 10x10x10 grid. editor__detector is a node which is attached to a free floating camera. It is used to place objects relative to it. First I get its position, and then update each position component, so it is rounded to tens (this.cellSize = 10);

For example, if x pos === 23.5:
23.5 / 10 = 2.35;
round(2.35) = 2;
2 * 10 = 20;
Component x is updated from 23.5 to 23. If initial value was 2.5 it will be rounded to 30 the same way.

You can regulate the grid side with different cellSize value. If it's 0.1 for example, and x position is 23.56
23.56 / 0.1 = 235.6;
round(235.6) = 236;
236 * 0.1 = 23.6;


Hope, you got the idea.


Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |