Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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 2415 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Wink but I am working hard on it.

      if (attackingPRIMARY) {
         if (atPF >= atPS) {
            attackingPRIMARY = false;
            atPF = 0;
            primaryRot = getRotation();
         } else {
            primaryRot += MathUtils.sin(atPF);
                                //primaryRot = getRotation() + MathUtils.sin(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

Offline 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"
Offline 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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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?
Offline 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  

EgonOlsen (78 views)
2018-06-10 19:43:48

EgonOlsen (58 views)
2018-06-10 19:43:44

EgonOlsen (78 views)
2018-06-10 19:43:20

DesertCoockie (260 views)
2018-05-13 18:23:11

nelsongames (158 views)
2018-04-24 18:15:36

nelsongames (157 views)
2018-04-24 18:14:32

ivj94 (899 views)
2018-03-24 14:47:39

ivj94 (162 views)
2018-03-24 14:46:31

ivj94 (812 views)
2018-03-24 14:43:53

Solater (175 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!