Topdown Sword Swing

I have a player image which I draw on top of a sword/arm image that I would like to rotate in a swinging motion on click. I figured using sin would be the best approach, however I somehow can’t get that to work very well…
Here is my code, which I wrote knowing that it wouldn’t work. I am still a little mathematically challenged :wink: but I am working hard on it.
Code:


		if (attackingPRIMARY) {
			if (atPF >= atPS) {
				attackingPRIMARY = false;
				atPF = 0;
				primaryRot = getRotation();
			} else {
				primaryRot += MathUtils.sin(atPF);
                                //primaryRot = getRotation() + MathUtils.sin(atPF);
				atPF++;
			}
		}

any ideas on how I could get a nice swing going? When I draw, I use primaryRot for the rotation in case you didn’t guess. Thanks

-cMp

I might be missing something, but why not just use primaryRot += 0.5f; // .5f for degrees per update (randomly chosen number) depending upon what looks good given your frame rate/game rate and whatnot.

I don’t fully understand, why the need for sin at this point? Unless you want it to rotate/swing at variable speed during the swing?

Then just when you are drawing it, make sure you include like
batch.draw(…, primaryRot);
so it draws the reflected rotation?

Well I’d like to produce a nice swing forward, slow, swing back.

Therefore I thought the sin of the current iteration would produce something along that?

Puts physics hat on
The acceleration of the sword depends on the character’s force plus gravity. Think of the sword/arm combination as a line segment of uniform density. It’s center of mass is its midpoint, which makes calculations convenient because you can sword as a vector to its midpoint. Torque determines the angular acceleration of a spinning object. The torque on the sword due to gravity is the cross product of the sword-vector and a gravity vector.

The acceleration of the sword (primaryRot) is proportional to characterStrength + crossZ(sword, grav).

Intuitively, the rotational force due to gravity is zero when the sword is straight up or straight down, and greatest when it is parallel to the ground. Because cross product in two dimensions is the length of two vectors (both of which, gravity and sword, are constant), times the sine of the angle between them, you can represent that force as k * sin(angle). So you are right to use sine, but you also need to add in the player’s strength.

There is no simple way to get the combination correct using just a few functions, but if you understand vector math you could do it easily.

But that probably is overkill. Why not just hardcode the angle of the sword at each frame after clicking?

Hey that stuff is great to know! Thanks alot, but I decided on a different approach for other attack types to take place, and as this will be a very fast paced game I can’t do what I was thinking.