# Block Creator does not works well

Hey guys, it’s me, again

I’m having problems with my block creator code. It works well, but when the coordinate is 0.0, it create the block on a grid cerll next to it’s place.

I’m using a “round” method for creating the box in the right place, but as I said, I’m having some problems with the 0.0 coordinates.

[java][/java][/java]protected float round(float num)

{

BigDecimal big = new BigDecimal(num);

float result;

big.setScale(1, BigDecimal.ROUND_DOWN);

result = big.intValue();

System.out.println(result);

if(result < 0) result -= 0.5f;

else result += 0.5f;

System.out.println(result);

return result;

}[java][/java]

What do you intend to exactly do with that method?

Create a block in a cell of a grid. It does create the block, but some blocks, that are being created on the 0.0 coordinates, does not work properly. When adding a block in a 0 coordinate (x and y axis), the block is placed in a cell next to the right place! If you want some picture tell meh. I really think the problem is in the round method!

[java]int x=0; int z=0; // where you want the box

Vector3f boxPosition = new Vector3f((float)x, 0, (float)z);

[/java]

Use integer coordinates for your box grid (assuming you don’t want off-grid boxes) so you dont have a rounding problem.

But some places in the grid aren’t int values!

The whole class, so you could try by yourself:

[java]public class AppState extends SimpleApplication {

private BulletAppState bulletAppState;

///-- Scene

Node scene;

Material mat_grid;

///-- Crosshair

Picture crosshair;

public static void main(String[] args) {

AppState thelab = new AppState();

thelab.start();

}

public void simpleInitApp() {

scene = new Node(“Scene”);

setupWorld();

setupKeys();

setupGUI();

}

{

mat_grid = new Material(assetManager, “Common/MatDefs/Misc/SimpleTextured.j3md”);

floortexture.setWrap(WrapMode.Repeat);

mat_grid.setTexture(“m_ColorMap”, floortexture);

}

public void setupWorld()

{

///-- Setup Physics Space

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

flyCam.setMoveSpeed(70f);

///-- Objects

scene.attachChild(makeFloor());

rootNode.attachChild(scene);

///-- Simple Light

PointLight light = new PointLight();

light.setPosition(new Vector3f(0, 10, 0));

light.setColor(ColorRGBA.White);

}

public void setupKeys()

{

}

private ActionListener actionListener = new ActionListener() {

@Override

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“Create”) && !keyPressed) {

CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());

scene.collideWith(ray, results);

if (results.size() > 0) {

CollisionResult closest = results.getClosestCollision();

scene.attachChild(makeBlock(“Block”,

round(closest.getContactPoint().x),

closest.getGeometry().getLocalTranslation().y,

round(closest.getContactPoint().z)));

rootNode.attachChild(scene);

}

}

}

};

public void setupGUI()

{

///-- Clear all children to start display the gui.

guiNode.detachAllChildren();

///-- Crosshair

crosshair = new Picture(“Crosshair”);

crosshair.scale(20);

crosshair.setImage(assetManager, “Interface/crosshair.png”, true);

crosshair.setPosition(settings.getWidth() / 2 - 10, settings.getHeight() / 2 - 10);

guiNode.attachChild(crosshair);

}

@Override

public void simpleUpdate(float tpf) {

}

///-- Make Floor

protected Geometry makeFloor() {

Box box = new Box(new Vector3f(0f, -.1f, 0f), 10, .1f, 10);

box.scaleTextureCoordinates(new Vector2f(20f, 20f));

Geometry floor = new Geometry(“Floor”, box);

floor.setMaterial(mat_grid);

return floor;

}

///-- Make Block

protected Geometry makeBlock(String name, float x, float y, float z) {

Box box = new Box(new Vector3f(x, y+0.5f, z), 0.5f, 0.5f, 0.5f);

box.scaleTextureCoordinates(new Vector2f(1f, 1f));

Geometry block = new Geometry(name, box);

block.setMaterial(mat_grid);

return block;

}

///-- Round

protected float round(float num)

{

BigDecimal big = new BigDecimal(num);

float result;

big.setScale(1, BigDecimal.ROUND_DOWN);

result = big.intValue();

System.out.println(result);

if(result < 0) result -= 0.5f;

else result += 0.5f;

System.out.println(result);

return result;

}

}[/java]

Now I see/remember what you want to do - the grid you are talking about is a texture of a grid on the floor (or wall) and when you click a texture you want a box to appear there the size of your grid (just over a grid square), right?

Did you work out your location/size of the grid square you clicked? From what I can see from the code is that you click somwhere on the floor texture and then want to round it to the nearest integer vector location. Is that the idea? I thought you wanted to have the box corners on the grid corners.

If so, why not using intVal = Math.round(floatVal) ?

I can’t test the example - I don’t have the .png files but when I use my own all I get is a black screen with a crosshair.

Thanks anyway, I gived up about it.