Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Topdown Sword Swing  (Read 1193 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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
 Games published by our own members! Check 'em out!
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.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (30 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (43 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (65 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x wessles 15x Spasi 12x alwex 11x 65K 11x kingroka123 11x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x Riven 7x chrislo27 7x KevinWorkman 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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