Is it possible to carve objects?

I am making a game in which I want to be able to carve objects with another object using the Boolean modifier written in python:

bpy.ops.object.modifier_add(type='BOOLEAN')
bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["Cone"]
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Boolean")


It works fine when I start the game with the cone inside the cube like seen in the picture (I moved the cone out of the cube during the game to have a better look) but if I move the cone into the cube during the game it doesn’t do anything. I joined a collision sensor to the python script, but later I noticed that the code runs the same way without any sensors attached to it.
Can someone please help.

There are two main types of code at work in Blender.
BPY deals with stuff inside blender as a modelling and animation suite.

BGE deals with game engine stuff.

You can’t really use BPY code in the game engine, maybe it sometimes seems to work, but it’s just not compatible.

Using a high density mesh, rays and mesh proxy it might be possible to make the illusion of carving, but it’d be pretty slow and easy to turn the mesh inside out unless you were careful. It’d also take mad skillz to code it. :slight_smile:

yeah, it would be neat to have things like slice as functions in bge,
that handle all the bpy themselves,

like extrude face, Boolean, slice, etc.
but I would not hold my breath.

Thanks guys for the quick answers.
So basically BPY is not suitable for the game engine.
And the slicing sounds interesting but I think it’s a bit advanced for me.

I think you can use the Bpy module, but any logic making use of it is not going to work if you save the game out as a standalone file.

The BGE has not had a whole lot of functionality relating to direct manipulation and creation of geometry since its inception, but getting that type of thing committed needs to consider more than just creating faces with a material (you would need to also have it support collisions, ray detection, rigid body simulation, and all the other stuff to make it real useful).

<b>from</b> <b>bge</b> <b>import</b> logic
cont = logic.getCurrentController()
object = cont.owner

<b>for</b> mesh <b>in</b> object.meshes:
   <b>for</b> m_index <b>in</b> range(len(mesh.materials)):
      <b>for</b> v_index <b>in</b> range(mesh.getVertexArrayLength(m_index)):
         vertex = mesh.getVertex(m_index, v_index)
         
<i>         # Do something with vertex here...</i>
         <i># ... eg: color the vertex red.</i>          
         vertex.color = [1.0, 0.0, 0.0, 1.0]

        # or change position 
        vertex.setXYZ(<i>[0,0,1]</i>)
        # you can also vertex.x = 10

Mesh:
http://www.blender.org/api/blender_python_api_2_73_5/bge.types.KX_MeshProxy.html#bge.types.KX_MeshProxy
Vertex:
http://www.blender.org/api/blender_python_api_2_73_5/bge.types.KX_VertexProxy.html#bge.types.KX_VertexProxy

A object has meshes , which have materials, which have vertices.
But how you determine which vertex you want to move, is up to you.
You probably check all vertices, whether the vertex positions are inside the other mesh, and then with some math move them somewhere.

Sorry for not being active on the forum for a while now.

Thanks for the help VegetableJuiceF, I’ll try that, but I think its a bit hard for me (I don’t know much of python), and its not exactly what I was looking for, but thanks very much anyway.
And if anyone has any other ideas please post it.

VegetableJuiceF:

You probably check all vertices, whether the vertex positions are inside the other mesh, and then with some math move them somewhere.

I am now working on something else and the code you sent would be very useful, unfortunately I am not much good at python and I don’t know how to check whether a vertex is inside another object, I did some research but didn’t find anything.
Can you please help (if possible please send a code about how to check if a vertex is inside another object) thanks

VegetableJuiceF:

You probably check all vertices, whether the vertex positions are inside the other mesh, and then with some math move them somewhere.

I am now working on something else and the code you sent would be very useful, unfortunately I am not much good at python and I don’t know how to check whether a vertex is inside another object, I did some research but didn’t find anything.
Can you please help (if possible please send a code about how to check if a vertex is inside another object) thanks.

Checking accurately whether a vertex is in a mesh is one of hard problems.
There is a definite performance problem also for this.

One way would be faking it.
Use simple shapes like (axis aligned) cubes or spheres where the math is easy, like vector distance for spheres or floored positions for cubes .
Have the cutting object be a simple object.
Map the volume of the cuttable complex mesh to smaller sections of those simpler shapes.

The simple idea:
Imagine minecraft.
Now imagine your object built in minecraft.
Kinda blocky, eh?
Now imagine an as blocky sword intersecting your object.



Which blocks are occupying the same positions?
Do they contain vertices?
If yes, then those vertices intersect the other objects volume.
It is as precise as the blocks are big.

In conclusion, see the first reply.
You either use what exists, build basically a voxel system or rethink what you want to achieve.

EDIT:
The initial idea to use spheres objects to push vertices :
( the sphere visible is only illustration, the the vertices are moved based on the object position)
http://i.imgur.com/tYLeaLg.gif
W - up ; S- down

Attachments

test.blend (606 KB)

I seems it changes the uv cords / texture values.
So basically a realtime terain height map. (it even has some strings named grassMap, dirtmap…). So yeah, you can.

The problem really isn’t of how you displace them, but which and where to.
Terrain painters use simple ray cast. It just works on the surface.
But we were asked about volume intersection which means that we have to evaluate the whole mesh on both objects.

Thanks very much for the code you sent VegetableJuiceF, it helped me a lot.
And sorry about sending the post twice, I got a bit mixed up.
Thanks again