MCPcopy Create free account
hub / github.com/DanielChappuis/reactphysics3d / moveBodyWithMouse

Method moveBodyWithMouse

testbed/src/SceneDemo.cpp:629–657  ·  view source on GitHub ↗

Called when the user is moving a body with the mouse

Source from the content-addressed store, hash-verified

627
628// Called when the user is moving a body with the mouse
629void SceneDemo::moveBodyWithMouse(double mousePosX, double mousePosY) {
630
631 if (!mIsMovingBody) {
632
633 // Find the body and the position of the mouse on that body (with raycasting)
634 openglframework::Vector4 screenPoint1((mousePosX / mWindowWidth) * 2.0 - 1.0, ((mWindowHeight - mousePosY) / mWindowHeight) * 2.0 - 1.0, -1, 1);
635 openglframework::Vector4 screenPoint2((mousePosX / mWindowWidth) * 2.0 - 1.0, ((mWindowHeight - mousePosY) / mWindowHeight) * 2.0 - 1.0, 1, 1);
636 openglframework::Vector4 worldP1 = (mCamera.getTransformMatrix() * mCamera.getProjectionMatrix().getInverse()) * screenPoint1;
637 openglframework::Vector4 worldP2 = (mCamera.getTransformMatrix() * mCamera.getProjectionMatrix().getInverse()) * screenPoint2;
638 openglframework::Vector3 cameraPos = mCamera.getOrigin();
639 rp3d::Vector3 worldPoint1(worldP1.x, worldP1.y, worldP1.z);
640 rp3d::Vector3 worldPoint2(worldP2.x, worldP2.y, worldP2.z);
641 rp3d::Ray ray(worldPoint1, worldPoint2);
642 mPhysicsWorld->raycast(ray, this);
643 }
644
645 if (mMovingBody != nullptr) {
646 openglframework::Vector4 previousScreenPos(mLastMouseX / mWindowWidth, (mWindowHeight - mLastMouseY) / mWindowHeight, 0, 0);
647 openglframework::Vector4 currentScreenPos(mousePosX / mWindowWidth, (mWindowHeight - mousePosY) / mWindowHeight, 0, 0);
648 openglframework::Vector4 forceScreen = currentScreenPos - previousScreenPos;
649 openglframework::Vector4 f = mCamera.getTransformMatrix() * forceScreen * MOUSE_MOVE_BODY_FORCE;
650 rp3d::Vector3 force(f.x, f.y, f.z);
651 mMovingBody->applyWorldForceAtLocalPosition(force, mMovingBodyLocalPoint);
652 }
653
654 mLastMouseX = mousePosX;
655 mLastMouseY = mousePosY;
656 mIsMovingBody = true;
657}
658
659// Called when a mouse button event occurs
660bool SceneDemo::mouseButtonEvent(int button, bool down, int mods, double mousePosX, double mousePosY) {

Callers

nothing calls this directly

Calls 4

getOriginMethod · 0.80
getInverseMethod · 0.45
raycastMethod · 0.45

Tested by

no test coverage detected