NOOB Q: How to Remove a Constraint in the BGE

Hello,

I was looking for a way to remove a rigid body constraint from two objects in the BGE on the fly. Since the BGE is quite limited in what you can do through the UI, I had a look at some python documentation and found a reference to removeConstraint(Constraint_ID), but I can’t find any sample scripts that I can get to work. I’m very unfamiliar with python and have only run a few pre-written scripts before, so I’m not sure how to go about using this function.

Can someone show me, and maybe set up a simple demo blend file that calls it in a script?

Thank you for helping!

removeConstraint
removeConstraint(constraint_ID)Removes the constraint.

constraint_ID
Type: integer

Note:Save the constraintID when the constraint is created.
Note:This removes the constraint between 2 game objects (point and edge constraints). It does not remove vehicle constraints.

Sample Code
Part 1: Create a Point Constraint

######### PointConstraint.py

import PhysicConstraints Moduleimport PhysicsConstraints

get object listobjList = GameLogic.getCurrentScene().objects

get object named Obj_1obj1 = objList[“OBObj_1”]

get object named Obj_2obj2 = objList[“OBObj_2”]

use point constraint typeconstraintType = 1

get Obj_1 physics IDobj1_ID = obj1.getPhysicsId()

get Obj_2 physics IDobj2_ID = obj2.getPhysicsId()

Use bottom right front corner of obj1 for point positionpointPos_x = 1.0

pointPos_y = -1.0
pointPos_z = -1.0

create a point constraintconstraint = PhysicsConstraints.createConstraint( obj1_ID, obj2_ID,

[INDENT=4] constraintType,
pointPos_x, pointPos_y, pointPos_z)
[/INDENT]

get the constraint IDconstraint_ID = constraint.getConstraintId()

save the constraint ID as an Obj_1 variableobj1[“constraint_ID”] = constraint_ID

Part 2: Remove constraint script

########### RemoveConstraint.py

import PhysicConstraints Moduleimport PhysicsConstraints

get object listobjList = GameLogic.getCurrentScene().objects

get object 1obj1 = objList[“OBObj_1”]

get constraint ID that was saved as an Obj_1 variable# when the constraint was createdconstraint_ID = obj1[“constraint_ID”]

remove constraintPhysicsConstraints.removeConstraint(constraint_ID)