0
$\begingroup$

I saw a game demo where a wooden crate broke into planks when it was hit by an object. I want to do something similar. I assume it's done with constraints, and I found a reference to a python function for removing constraints (called removeConstraint()) but I'm unfamiliar with python scripts so I don't know how to call it and I can't find any examples. The reference is this function is here:

http://www.tutorialsforblender3d.com/GameModule/ClassKX_PyConstraintBinding_4.html

Can anyone help me to understand how to use this function, or how else I might create breakable crates?

$\endgroup$

1 Answer 1

1
$\begingroup$

Most often, objects breaking up is done by, when hit by something, ending the original object and replacing it with one or more rigid bodies. (ie using add object/end object actuators or the python equivalent)

Now, on to constraints. I know no way to get the constraint if it was made in the user interface. There simply doesn't seem to be a method to get constraints. But if you create the constraint from python and store it's ID's, then you can manipulate it/destroy it.

To create a constraint (this is for a wheel, free to rotate in one axis):

def joinWheel(wheel, axle):
    obj1 = wheel
    obj2 = axle

    id1 = obj1.getPhysicsId()
    id2 = obj2.getPhysicsId()

    '''bge.constraints.createConstraint(physics_id_1, physics_id_2,
                             constraint_type,
                             edge_position_x, edge_position_y, edge_position_z,
                             edge_angle_x, edge_angle_y, edge_angle_z)'''

    obj1['joint'] = bge.constraints.createConstraint(id1, id2,
                             bge.constraints.GENERIC_6DOF_CONSTRAINT,
                             0,0,0,0,0,0)

    obj1['joint'].setParam(0, 0.0, 0.0) #No X translation
    obj1['joint'].setParam(1, 0.0, 0.0) #No Y translation
    obj1['joint'].setParam(2, 0.0, 0.0) #No Z translation                     
    obj1['joint'].setParam(3, 0, 0) #No X Rotation
    obj1['joint'].setParam(4, 0, 0) #No Y Rotation
    #obj1['joint'].setParam(5, 0, 0) #Commented out so there is z rotation

And now we have a reference to the constraint stored in objs['joint'] which can be removed with:

def removeWheel(wheel):
    bge.constraints.removeConstraint(wheel['joint'].getConstraintId())
$\endgroup$
6
  • $\begingroup$ Thank you very much for this answer. But why does that one line of code in the middle have triple quotes around it? I don't understand that. and I'm pretty sure it's not python syntax. $\endgroup$ Commented Jun 1, 2016 at 20:19
  • $\begingroup$ Triple quotes are multi-line strings. In this case I'm using it as a comment to show what each parameter is. So, it is python syntax, but because I don't assign it to a variable, it doesn't do anything. $\endgroup$
    – sdfgeoff
    Commented Jun 1, 2016 at 22:39
  • $\begingroup$ Okay, I just want to be sure. Will this run as a script in the BGE just as it is, as long as I have two objects named wheel and axle? Or are those defined internally? I'm still confused as to how it knows what objects to target. $\endgroup$ Commented Jun 1, 2016 at 23:52
  • $\begingroup$ No. These are functions that have to be called from a larger python script. You can see a small vehicle demo I made with a very similar joinWheel function over here: dl.dropboxusercontent.com/u/17630723/Blends/… $\endgroup$
    – sdfgeoff
    Commented Jun 2, 2016 at 10:34
  • $\begingroup$ But as I said, constraints are not the typical use case for breaking crates. I'll see if I can write a more comprehensive answer on the switch-objects method this coming weekend. $\endgroup$
    – sdfgeoff
    Commented Jun 2, 2016 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .