Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Topdown Sword Swing  (Read 2370 times) 0 Members and 1 Guest are viewing this topic.
Slyth2727
 « Posted 2013-08-22 20:50:03 »

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 but I am working hard on it.
Code:

 1  2  3  4  5  6  7  8  9  10  11 `      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
namrog84

JGO Ninja

Medals: 46
Projects: 4

Keep programming!

 « Reply #1 - Posted 2013-08-22 20:53:34 »

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?

"Experience is what you get when you did not get what you wanted"
Slyth2727
 « Reply #2 - Posted 2013-08-22 20:55:50 »

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?
Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #3 - Posted 2013-08-22 21:50:00 »

*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?
Slyth2727
 « Reply #4 - Posted 2013-08-22 22:10:36 »

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.
Pages: [1]
 ignore  |  Print

 ivj94 (585 views) 2018-03-24 14:47:39 ivj94 (49 views) 2018-03-24 14:46:31 ivj94 (383 views) 2018-03-24 14:43:53 Solater (63 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (704 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (494 views) 2017-12-31 17:17:51 xxMrPHDxx (734 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org