Adding blocks in Game

benboesel posted this 10 June 2015

Hello two quick questions if anyone can help!

When you go to edit a Volume in-editor, there's a transparent voxel that glides on the surface that helps you more confidently place voxels. In-game, I am using the raycast method (which works perfectly) used in the RPG demo to place blocks, but it lacks this ghost block as a guide for placing objects. Could someone describe how I can implement this in game?

In my game I'd be amazing to also add blocks in bulk. Is there a way, in game, to define a volume (like a cube or sphere) and fill it with voxels? Sort of like a large brush to paint the voxels in.

Thanks soooo much!

Order By: Standard | Newest | Votes
GarethIW posted this 17 June 2015

Okay, this bit of code is a mish-mash of the raycasting from the PicaIslands demo combined with the code I use in the editor to get a position to draw a cursor at. It's not working code, but it should give you an idea of how to do what you need.

Basically, you should do this after the ray has hit an active voxel if you are looking to place a cursor next to it for adding voxels. If you want a cursor for removing or recolouring a voxel, you shouldn't do the stepping back bit.

// d is our position along the ray
// we know we've hit an active voxel, so take one step back along the ray to the last empty voxel position
d -= pvo.VoxelSize*0.1f;

// Get the inactive voxel so we can use it later for painting
Voxel? inactiveVoxel = pvo.GetVoxelAtWorldPosition(r.GetPoint(d));

// Now get a cursor position
Vector3 cursorPosition = pvo.transform.TransformPoint(((pvo.GetVoxelPosition(r.GetPoint(d)) * pvo.VoxelSize) - pvo.Pivot) + ((*pvo.VoxelSize)*0.5f));

Now you have a cursor position, you can draw a transparent cube or whatever at the location. I'd suggest a separate gameobject for this.

For your second question (shape painting): Once you have the cursor position, you can get the array position of that voxel using GetVoxelArrayPosition().

Once you have an array position, you can loop through a grid using your initial array position as a centre point. You can then use SetVoxelAtArrayPosition to set the voxels within your grid (SetVoxelAtArrayPosition performs a bounds check so you don't need to worry about your grid co-ords being outside of the voxel array).

If you wanted to do a spherical shape, you'd simply use Vector3.Distance to see how far your current grid position is from your cursor position centre point.

I realise it's not a copy+paste solution, but this is the kind of thing that requires programming chops to achieve. I've hopefully explained the PicaVoxel-specific bits enough to get you started.

Our 137 members have posted 527 times in 128 discussions