1
$\begingroup$

I have a cube and a plane in an inactive layer. I copy them to another layer by using addObject and change their vertices by setXYZ. The plane is static and the cube is a rigidObject. When I start the simulation the cube goes through the plane, while it should not happen since the cube is shown right on top of the plane. Could someone let me know what I am doing wrong? Below is part of the code that I wrote. I wrote it in a way that it runs just once and it doesn't add a cube and plane at every cycle of bge (I removed those parts below):

tmpobj = scene.addObject("Cube", "Empty", 0)
tmpobj.worldPosition = box_world # box_world is a variable that specifies where the cube should move to in the active layer.
mesh = tmpobj.meshes[0]
nvert = mesh.getVertexArrayLength(0)

cc = 0
for j in range(nvert):
    vert = mesh.getVertex(0, j)    
    vert.setXYZ(box[cc:cc+3]) # box is a 1x72 array that contains the new coordinates of the box
    cc = cc + 3

tmpobj2 = scene.addObject("Plane", "Empty", 0)
tmpobj2.worldPosition = floor_world # floor_world is a variable that contains the world position of the plane in the active layer

mesh = tmpobj2.meshes[0]
nvert = mesh.getVertexArrayLength(0)
for j in range(nvert):
   vert = mesh.getVertex(0, j)
   vert.setXYZ(floorcorners) # floorcorner is a variable that contains the coordinates of the floor plane
$\endgroup$
  • $\begingroup$ You need to set up physics properties at your Cube as Ridgid Body or Dynamic and at Plane as Static so that they can interact... $\endgroup$ – Samoth Apr 16 '16 at 9:12
1
$\begingroup$

The first thing I see, is that you are loading both objects at the exact same point, "Empty". If it's the default cube and plane primitives, then you're spawning your cube halfway below the plane. Then your changing the vertices of the object, not the global position. This is basically saying "render the object at one location, but render the mesh at another.

This image shows, the collision box offset from the mesh renderer, because the vertex positions where modified, not object position:

Collision box in different location than mesh

So, chances are, your collider is still halfway through the plane, while the mesh is offset the distance you specify.

If you remove the part of your script that moves verts, and just adjust the global position, the box will rest on the plane.

Is there a reason your moving just the mesh(verts) and not the entire object?

If you need help seeing what's going on, you can turn on 'Collision Bounds' in the 'Physics Tab' of 'Properties Panel' for both the Cube and Plane. Also, enable 'Physics Visualization' in the 'Render Tab'.

Collision BoundsEnable Physics Visual

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.