Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
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 966 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.
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?
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?

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!
Legends of Yore - The Casual Retro Roguelike
Offline Several Kilo-Bytes

Senior Member


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.

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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
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!