MCPcopy Create free account
hub / github.com/defold/defold / Physics_CreateJoint

Function Physics_CreateJoint

engine/gamesys/src/gamesys/scripts/script_physics.cpp:695–731  ·  view source on GitHub ↗

create a physics joint * * Create a physics joint between two collision object components. * * Note: Currently only supported in 2D physics. * * @name physics.create_joint * @param joint_type [type:number] the joint type * @param collisionobject_a [type:string|hash|url] first collision object * @param joint_id [type:string|hash] id of the joint * @

Source from the content-addressed store, hash-verified

693 *
694 */
695 static int Physics_CreateJoint(lua_State* L)
696 {
697 DM_LUA_STACK_CHECK(L, 0);
698
699 dmPhysics::JointType type = (dmPhysics::JointType)luaL_checkinteger(L, 1);
700 if (type >= dmPhysics::JOINT_TYPE_COUNT)
701 {
702 return DM_LUA_ERROR("unknown joint type: %d", type);
703 }
704
705 dmhash_t joint_id = dmScript::CheckHashOrString(L, 3);
706 dmVMath::Point3 pos_a = dmVMath::Point3(*dmScript::CheckVector3(L, 4));
707 dmVMath::Point3 pos_b = dmVMath::Point3(*dmScript::CheckVector3(L, 6));
708
709 dmGameObject::HCollection collection = dmGameObject::GetCollection(CheckGoInstance(L));
710
711 CollisionComponent* comp_a = 0x0;
712 CollisionWorld* comp_world_a = 0x0;
713 GetCollisionObject(L, 2, collection, &comp_a, &comp_world_a);
714 CollisionComponent* comp_b = 0x0;
715 CollisionWorld* comp_world_b = 0x0;
716 GetCollisionObject(L, 5, collection, &comp_b, &comp_world_b);
717
718 if (comp_world_a != comp_world_b) {
719 return DM_LUA_ERROR("joints can only be connected to collision objects within the same physics world");
720 }
721
722 dmPhysics::ConnectJointParams params(type);
723 UnpackConnectJointParams(L, type, 7, params);
724 dmPhysics::JointResult r = dmGameSystem::CreateJoint(comp_world_a, comp_a, joint_id, pos_a, comp_b, pos_b, type, params);
725 if (r != dmPhysics::RESULT_OK) {
726 return DM_LUA_ERROR("could not create joint: %s (%d)", PhysicsResultString[r], r);
727 }
728
729 return 0;
730
731 }
732
733 /*# destroy a physics joint
734 *

Callers

nothing calls this directly

Calls 9

luaL_checkintegerFunction · 0.85
CheckHashOrStringFunction · 0.85
CheckVector3Function · 0.85
GetCollectionFunction · 0.85
CheckGoInstanceFunction · 0.85
UnpackConnectJointParamsFunction · 0.85
CreateJointFunction · 0.85
GetCollisionObjectFunction · 0.70
Point3Class · 0.50

Tested by

no test coverage detected