Jump to content
ErhanK

Weapon Rotation

Recommended Posts

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();
	}

 

 

 

Share this post


Link to post

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.)

Share this post


Link to post
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);

 

Share this post


Link to post
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.

Share this post


Link to post

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.

Share this post


Link to post

Thank you, josh. I tried, but the result is the same. I need to think more about it.

 

Share this post


Link to post

I don't think I understand what you are trying to do.

Share this post


Link to post

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.

Share this post


Link to post

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.

 

Share this post


Link to post

Of course. In the upper left corner there are rotation values related to the weapon.

 

  • Like 2

Share this post


Link to post

Very cool. That's a nice subtle detail that improves the feel of it.

  • Thanks 1

Share this post


Link to post

Looks great! Better than just the weapon tilting a bit as the character rotates.

  • Like 1

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...