dennis.chen Posted February 28, 2013 Share Posted February 28, 2013 Hi, I have added PhysicalTrigger through scrpit like below: trigger = add_editor( new PhysicalTrigger( Shape_BOX, position ) ); trigger.setEnterCallback("enterCallBack"); trigger.setLeaveCallback("leaveCallBack"); void enterCallback(Body body) { Object object = body.getObject(); object.setMaterialState("ambient",2,0); } void leaveCallback(Body body) { Object object = body.getObject(); object.setMaterialState("ambient",1,0); } after that ,i added physics for each object through scrpit.when the trigger enter or leave some object,it could be detected. then,i remove these physics through sripts like below and re-added physics when the trigger was inside one of the objects: foreach(Node node; nodes) { if (node.getType()==NODE_OBJECT_MESH) { Object obj=node_cast(node); Body body=obj.getBody(); obj.setBody(NULL); Shape shape=body.getShape(0);//only add one physics body.removeShape(shape); delete class_append(shape); delete class_append(body); } } after "re-added physics" operation,system fire leaveCallback but the parameter of "body" was invalid and caused the application to crash. did i remove physics incorrect? thanks. Link to comment
manguste Posted February 28, 2013 Share Posted February 28, 2013 Dennis, What caught my eye is that you delete a shape. Body manages the shape, so delete only a body instead. Link to comment
dennis.chen Posted March 1, 2013 Author Share Posted March 1, 2013 hi ,maguste, as you mentioned,deleting shapes is redundant. As far as my understanding, when the physics trigger enters the object, an enterCallback will be triggered. If at this time I delete this body and re-add another body into the object, it will fire a leaveCallback when updateContacts() calls. However, the problem is the previous body from the entercallback to the leavecallback has already been deleted and therefore it leads to a crash. So instead what I did is something like this and the app run well: 1.clear bodies 2.set trigger callbacks to NULL 3.updateContacts 4.reset trigger callbacks to previous calls. thank you . Link to comment
manguste Posted March 2, 2013 Share Posted March 2, 2013 If you got it right, that's great. For any future cases, please provide a minimal test scene for reproduction. This way we can check it if it's a bug on our side or (since it's minimal) you go against UnigineScript syntax or logic. Or you can get what's wrong right while creating if :) Link to comment
Recommended Posts