RID DampedSpringJoint2D::_configure_joint(){ Node *node_a = has_node( get_node_a() ) ? get_node( get_node_a() ) : (Node*)NULL; Node *node_b = has_node( get_node_b() ) ? get_node( get_node_b() ) : (Node*)NULL; if (!node_a || !node_b) return RID(); PhysicsBody2D *body_a=node_a->cast_to<PhysicsBody2D>(); PhysicsBody2D *body_b=node_b->cast_to<PhysicsBody2D>(); if (!body_a || !body_b) return RID(); if (get_exclude_nodes_from_collision()) Physics2DServer::get_singleton()->body_add_collision_exception(body_a->get_rid(),body_b->get_rid()); else Physics2DServer::get_singleton()->body_remove_collision_exception(body_a->get_rid(),body_b->get_rid()); Matrix32 gt = get_global_transform(); Vector2 anchor_A = gt.get_origin(); Vector2 anchor_B = gt.xform( Vector2(0,length) ); RID dsj = Physics2DServer::get_singleton()->damped_spring_joint_create(anchor_A,anchor_B,body_a->get_rid(),body_b->get_rid()); if (rest_length) Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj,Physics2DServer::DAMPED_STRING_REST_LENGTH,rest_length); Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj,Physics2DServer::DAMPED_STRING_STIFFNESS,stiffness); Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj,Physics2DServer::DAMPED_STRING_DAMPING,damping); return dsj; }
RID PinJoint2D::_configure_joint() { Node *node_a = has_node( get_node_a() ) ? get_node( get_node_a() ) : (Node*)NULL; Node *node_b = has_node( get_node_b() ) ? get_node( get_node_b() ) : (Node*)NULL; if (!node_a && !node_b) return RID(); PhysicsBody2D *body_a=node_a ? node_a->cast_to<PhysicsBody2D>() : (PhysicsBody2D*)NULL; PhysicsBody2D *body_b=node_b ? node_b->cast_to<PhysicsBody2D>() : (PhysicsBody2D*)NULL; if (!body_a && !body_b) return RID(); if (!body_a) { SWAP(body_a,body_b); } else if (body_b) { //add a collision exception between both if (get_exclude_nodes_from_collision()) Physics2DServer::get_singleton()->body_add_collision_exception(body_a->get_rid(),body_b->get_rid()); else Physics2DServer::get_singleton()->body_remove_collision_exception(body_a->get_rid(),body_b->get_rid()); } RID pj = Physics2DServer::get_singleton()->pin_joint_create(get_global_transform().get_origin(),body_a->get_rid(),body_b?body_b->get_rid():RID()); Physics2DServer::get_singleton()->pin_joint_set_param(pj, Physics2DServer::PIN_JOINT_SOFTNESS, softness); return pj; }
RID GrooveJoint2D::_configure_joint(){ Node *node_a = has_node( get_node_a() ) ? get_node( get_node_a() ) : (Node*)NULL; Node *node_b = has_node( get_node_b() ) ? get_node( get_node_b() ) : (Node*)NULL; if (!node_a || !node_b) return RID(); PhysicsBody2D *body_a=node_a->cast_to<PhysicsBody2D>(); PhysicsBody2D *body_b=node_b->cast_to<PhysicsBody2D>(); if (!body_a || !body_b) return RID(); if (get_exclude_nodes_from_collision()) Physics2DServer::get_singleton()->body_add_collision_exception(body_a->get_rid(),body_b->get_rid()); else Physics2DServer::get_singleton()->body_remove_collision_exception(body_a->get_rid(),body_b->get_rid()); Matrix32 gt = get_global_transform(); Vector2 groove_A1 = gt.get_origin(); Vector2 groove_A2 = gt.xform( Vector2(0,length) ); Vector2 anchor_B = gt.xform( Vector2(0,initial_offset) ); return Physics2DServer::get_singleton()->groove_joint_create(groove_A1,groove_A2,anchor_B,body_a->get_rid(),body_b->get_rid()); }
void Joint2D::_update_joint(bool p_only_free) { if (joint.is_valid()) { if (ba.is_valid() && bb.is_valid()) Physics2DServer::get_singleton()->body_remove_collision_exception(ba, bb); Physics2DServer::get_singleton()->free(joint); joint = RID(); ba = RID(); bb = RID(); } if (p_only_free || !is_inside_tree()) return; Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)NULL; Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)NULL; if (!node_a || !node_b) return; PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a); PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b); if (!body_a || !body_b) return; if (!body_a) { SWAP(body_a, body_b); } joint = _configure_joint(body_a, body_b); if (!joint.is_valid()) return; Physics2DServer::get_singleton()->get_singleton()->joint_set_param(joint, Physics2DServer::JOINT_PARAM_BIAS, bias); ba = body_a->get_rid(); bb = body_b->get_rid(); Physics2DServer::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision); }