ErhanK Posted March 17, 2019 Share Posted March 17, 2019 Hi. I would like to make the rotation of the gun at -45 and +45 (or less) degrees relative to that of the camera. When I limit the values, it only works according to the world coordinate. For example, when I turn a full turn to the left, I have to turn a full turn right again so that the gun rotates in the right direction. I want to do a gun control like Insurgency game. How can I fix this? Here is my code. float currentTime = Time::GetCurrent(); Vec2 currentMousePos = Window::GetCurrent()->GetMousePosition(); Window::GetCurrent()->SetMousePosition(Math::Round(Context::GetCurrent()->GetWidth() / 2), Math::Round(Context::GetCurrent()->GetHeight() / 2)); Vec2 centerPos = Window::GetCurrent()->GetMousePosition(); currentMousePos.x = Math::Round(currentMousePos.x); currentMousePos.y = Math::Round(currentMousePos.y); mouseDifference.x = Math::Curve(currentMousePos.x - centerPos.x, mouseDifference.x, 2 / Time::GetSpeed()); mouseDifference.y = Math::Curve(currentMousePos.y - centerPos.y, mouseDifference.y, 2 / Time::GetSpeed()); camRotation.x = Math::Clamp(camRotation.x + mouseDifference.y / mouseSensitivity, -90, 90); camRotation.y = camRotation.y + (mouseDifference.x / mouseSensitivity); hurtOffset.x = Math::Inc(0, hurtOffset.x, 2 * Time::GetSpeed()); hurtOffset.y = Math::Inc(0, hurtOffset.y, 2 * Time::GetSpeed()); smoothedHurtOffset.x = Math::Curve(hurtOffset.x, smoothedHurtOffset.x, 3); smoothedHurtOffset.y = Math::Curve(hurtOffset.y, smoothedHurtOffset.y, 3); bobOffset.x = Math::Inc(0, bobOffset.x, Time::GetSpeed() * 0.8f); bobOffset.y = Math::Inc(0, bobOffset.y, Time::GetSpeed()); smoothedBobOffset.x = Math::Curve(bobOffset.x, smoothedBobOffset.x, 25); smoothedBobOffset.y = Math::Curve(bobOffset.y, smoothedBobOffset.y, 25); weaponViewRot.x = Math::Clamp(camRotation.x + mouseDifference.y / mouseSensitivity, -45, 45); weaponViewRot.y = Math::Clamp(camRotation.y + mouseDifference.x / mouseSensitivity, -45, 45); smoothWeaponViewRot.x = Math::Curve(weaponViewRot.x, smoothedHurtOffset.x, 5); smoothWeaponViewRot.y = Math::Curve(weaponViewRot.y, smoothedHurtOffset.x, 5); playerCamera->SetRotation(camRotation + smoothedBobOffset + smoothedHurtOffset); testWeapon->entity->SetRotation(camRotation + smoothWeaponViewRot); if (Window::GetCurrent()->MouseDown(1)) { testWeapon->weaponShoot(); } Quote Link to comment Share on other sites More sharing options...
gamecreator Posted March 17, 2019 Share Posted March 17, 2019 Unfortunately, SetRotation is designed to not always put you at the angle you specified. I don't know why but this is an intentional design choice. You can look up gimbal lock on these forums for a workaround. (To be fair, I didn't look at your code so I'm not sure if it's incorrect but yes, SetRotation has issues.) Quote Link to comment Share on other sites More sharing options...
Josh Posted March 25, 2019 Share Posted March 25, 2019 On 3/17/2019 at 9:51 AM, gamecreator said: Unfortunately, SetRotation is designed to not always put you at the angle you specified. I don't know why but this is an intentional design choice. You can look up gimbal lock on these forums for a workaround. (To be fair, I didn't look at your code so I'm not sure if it's incorrect but yes, SetRotation has issues.) The numbers might not come out the same but the rotation you specify is the one it will use. The problem is you can't really add rotations like that. Instead, do this: testWeapon->entity->SetRotation(camRotation); testWeapon->entity->Turn(smoothWeaponViewRot); Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
gamecreator Posted March 25, 2019 Share Posted March 25, 2019 8 hours ago, Josh said: The numbers might not come out the same All I'm saying is you could make this happen, if you chose to. The workaround everyone needs to discover and implement could be written straight into the engine to avoid all this confusion and make the function intuitive. The only downside I could see right now is that SetRotation would be slower. Maybe that's enough to not implement it or maybe there's more to it but I don't know. I also don't know how other engines do this; it could be the norm. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 25, 2019 Share Posted March 25, 2019 If you input local rotation, the rotation value is actually left exactly the same as what you put in, but if you input global rotation that rotation has to be transformed into local space. (The engine only stores a local rotation value.) Retrieving the rotation back in global space involves another transformation. Those numbers will not necessarily be the same, although they will represent the same orientation. For example, if you input 365 the engine will probably give you back 5 degrees, which is the same but corrected. The reason an entity does not store a global rotation value is because this would change every time anything in the parent hierarchy changes. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
ErhanK Posted March 25, 2019 Author Share Posted March 25, 2019 Thank you, josh. I tried, but the result is the same. I need to think more about it. Quote Link to comment Share on other sites More sharing options...
Josh Posted March 25, 2019 Share Posted March 25, 2019 I don't think I understand what you are trying to do. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
ErhanK Posted March 25, 2019 Author Share Posted March 25, 2019 I just want to make this move. (time 0.12) But this is only happening when I look at the global z direction. https://youtu.be/_6xEFmViZ8o?t=12 Quote Link to comment Share on other sites More sharing options...
Josh Posted March 25, 2019 Share Posted March 25, 2019 Well since camrotation is a rotation value in space and the mouse movement is sort of in "screen space" maybe you need to do something like this: weaponViewRot.x = camRotation.x + Math::Clamp(mouseDifference.y / mouseSensitivity, -45, 45); weaponViewRot.y = camRotation.y + Math::Clamp(mouseDifference.x / mouseSensitivity, -45, 45); You will have to experiment with that, but the point is to get your clamped offset before the camera rotation is added into it. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
ErhanK Posted March 25, 2019 Author Share Posted March 25, 2019 Well, I'll try after the slider problem that came with 4.6 :-) thanks Quote Link to comment Share on other sites More sharing options...
Josh Posted March 25, 2019 Share Posted March 25, 2019 2 minutes ago, ErhanK said: Well, I'll try after the slider problem that came with 4.6 :-) thanks See updated docs here: https://www.leadwerks.com/learn?page=API-Reference_Object_Widget_Slider 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
ErhanK Posted March 30, 2019 Author Share Posted March 30, 2019 this code solved the problem // tmpX and tmpY are global float x = 0; x = mouseDifference.y / mouseSensitivity; tmpX += x; if (tmpX > 20) { tmpX = 20; } else if (tmpX < -20) { tmpX = -20; } float y = 0; y = mouseDifference.x / mouseSensitivity; tmpY += y; if (tmpY > 60) { tmpY = 60; } else if (tmpY < -60) { tmpY = -60; } //CONVERT TO VECTOR Vec2 wpnRot; wpnRot.x = tmpX; wpnRot.y = tmpY; // SMOOTING smoothWeaponViewRot.x = Math::Curve(wpnRot.x, smoothedHurtOffset.x, 5); smoothWeaponViewRot.y = Math::Curve(wpnRot.y, smoothedHurtOffset.x, 5); //THEN testWeapon->entity->SetRotation(camRotation); //PLAYER ROTATION testWeapon->entity->Turn(smoothWeaponViewRot); //WEAPON VIEW ROTATION //now the weapon can navigate within the boundaries of the player's angle of view, according to the movement of the mouse. Quote Link to comment Share on other sites More sharing options...
slick29 Posted April 5, 2019 Share Posted April 5, 2019 @ErhanK Can we see an updated screen capture? 1 Quote Link to comment Share on other sites More sharing options...
ErhanK Posted April 6, 2019 Author Share Posted April 6, 2019 Of course. In the upper left corner there are rotation values related to the weapon. 2 Quote Link to comment Share on other sites More sharing options...
Josh Posted April 6, 2019 Share Posted April 6, 2019 Very cool. That's a nice subtle detail that improves the feel of it. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
ErhanK Posted April 7, 2019 Author Share Posted April 7, 2019 Thanks. I love this feature too. I had to do it.? Quote Link to comment Share on other sites More sharing options...
slick29 Posted April 8, 2019 Share Posted April 8, 2019 Looks great! Better than just the weapon tilting a bit as the character rotates. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.