void Urho2DConstraints::HandleTouchBegin3(StringHash eventType, VariantMap& eventData) { auto* graphics = GetSubsystem<Graphics>(); auto* physicsWorld = scene_->GetComponent<PhysicsWorld2D>(); using namespace TouchBegin; RigidBody2D* rigidBody = physicsWorld->GetRigidBody(eventData[P_X].GetInt(), eventData[P_Y].GetInt()); // Raycast for RigidBody2Ds to pick if (rigidBody) { pickedNode = rigidBody->GetNode(); auto* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite auto* rigidBody = pickedNode->GetComponent<RigidBody2D>(); // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with touch auto* constraintMouse = pickedNode->CreateComponent<ConstraintMouse2D>(); Vector3 pos = camera_->ScreenToWorldPoint(Vector3((float)eventData[P_X].GetInt() / graphics->GetWidth(), (float)eventData[P_Y].GetInt() / graphics->GetHeight(), 0.0f)); constraintMouse->SetTarget(Vector2(pos.x_, pos.y_)); constraintMouse->SetMaxForce(1000 * rigidBody->GetMass()); constraintMouse->SetCollideConnected(true); constraintMouse->SetOtherBody(dummyBody); // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D constraintMouse->SetDampingRatio(0); } SubscribeToEvent(E_TOUCHMOVE, URHO3D_HANDLER(Urho2DConstraints, HandleTouchMove3)); SubscribeToEvent(E_TOUCHEND, URHO3D_HANDLER(Urho2DConstraints, HandleTouchEnd3)); }
void Urho2DConstraints::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData) { auto* input = GetSubsystem<Input>(); auto* physicsWorld = scene_->GetComponent<PhysicsWorld2D>(); RigidBody2D* rigidBody = physicsWorld->GetRigidBody(input->GetMousePosition().x_, input->GetMousePosition().y_); // Raycast for RigidBody2Ds to pick if (rigidBody) { pickedNode = rigidBody->GetNode(); //log.Info(pickedNode.name); auto* staticSprite = pickedNode->GetComponent<StaticSprite2D>(); staticSprite->SetColor(Color(1.0f, 0.0f, 0.0f, 1.0f)); // Temporary modify color of the picked sprite // Create a ConstraintMouse2D - Temporary apply this constraint to the pickedNode to allow grasping and moving with the mouse auto* constraintMouse = pickedNode->CreateComponent<ConstraintMouse2D>(); constraintMouse->SetTarget(GetMousePositionXY()); constraintMouse->SetMaxForce(1000 * rigidBody->GetMass()); constraintMouse->SetCollideConnected(true); constraintMouse->SetOtherBody(dummyBody); // Use dummy body instead of rigidBody. It's better to create a dummy body automatically in ConstraintMouse2D } SubscribeToEvent(E_MOUSEMOVE, URHO3D_HANDLER(Urho2DConstraints, HandleMouseMove)); SubscribeToEvent(E_MOUSEBUTTONUP, URHO3D_HANDLER(Urho2DConstraints, HandleMouseButtonUp)); }