How to code a sword swipe?

Alright I’m back and I have a simple math question, see the picture

So O is the center point, A is the sword’s tip position, and A’ is the sword’s tip position after rotated by 45°.
The positions are in pixels and I use radians

The question is what is the formula to find the position A have after rotating this way? ;D

Do you mean you want to get back to position A after rotating 45 degrees? If so, just rotate -45 degrees or 315 degrees

I want to know what will be the position of A after rotating by 45°, from 0° to 45°

So just a simple rotation?

Translate so the origin of the rotation is @ [0,0], then apply the appropriate maths. (Silly old harry, caught a herring, trawling off America. :D)

Or better yet use whatever Math API you have to hand; the JDK can do rotation using AffineTransform.

Though inverting transforms like that is generally bad practice, as it’ll introduce fp imprecision.

The formula for any 2D rotation of a vector/position about a given rotation center ‘O’ is:


cos = Math.cos(angleInRadians) // cos(angle) is the cosine of the angle.
sin = Math.sin(angleInRadians) // sin(angle) is the sine of the angle.
xNew = cos * (x - O.x) + sin * (y - O.y) + O.x
yNew = cos * (x - O.x) + sin * (y - O.y) + O.y

So, insert for O.x = 602 and for O.y = 602, then:


cos = cos(45 degrees) = 1/sqrt(2) = 0.7071
sin = sin(45 degrees) = 1/sqrt(2) = 0.7071
xNew = 0.7071 * (1204 - 602) + 0.7071 * (602 - 602) + 602 = 1027
yNew = 0.7071 * (1204 - 602) + 0.7071 * (602 - 602) + 602 = 1027

But that is what you already have for A’, so I don’t understand your question. A is always A, and A’ is your transformed A.

Ok thanks :smiley: and yes I already had A’ because I checked it’s position by manually pointing it with my mouse ;D

I think there is an error with you formula


xNew = cos * (x - O.x) + sin * (y - O.y) + O.x
yNew = cos * (x - O.x) + sin * (y - O.y) + O.y

It doesn’t work with every angle, I modified it a bit a now it works everytime


xNew = cos * (x - O.x) - sin * (y - O.y) + O.x
yNew = sin * (x - O.x) + cos * (y - O.y) + O.y

Ah yes. You are absolutely right! Thanks for correcting. :wink:

Alright so I combined that formula to find the sword’s tip

xNew = cos * (x - O.x) - sin * (y - O.y) + O.x
yNew = sin * (x - O.x) + cos * (y - O.y) + O.y

With my polygon idea and it works AMAZING! I can’t wait to show you guys how it looks :smiley:

So now the sword can rotate and translate and the swipe will follow, but there is still one thing.

I have to find the formula to find the position of the tip of the sword after TWO rotation with DIFFERENT centre points, and has always, an image is better to explain.

http://image.noelshack.com/fichiers/2015/34/1439809576-rotation2.png

So first of all, look at the blue sword, it’s tip’s position is A
Then it rotate 45° with the BLUE dot as the centre point, it’s tip’s position is now B
Then it rotate 45° with now GREEN dot as the centre point, the tip position is now C.

And the question is what is the formula that return C? I tried multiple things with the above formula but none of my tries worked so I hope you are better at maths than me ;D

How 'bout this:


// Apply the first (outer) rotation
xNew = cos * (x - O.x) - sin * (y - O.y) + O.x
yNew = sin * (x - O.x) + cos * (y - O.y) + O.y
// Compute new center of rotation for second (inner) rotation
// which is the half between O and 'B'
O2.x = (xNew + O.x) / 2.0
O2.y = (yNew + O.y) / 2.0
// Apply the second (inner) rotation
xNew2 = cos * (xNew - O2.x) - sin * (yNew - O2.y) + O2.x
yNew2 = sin * (xNew - O2.x) + cos * (yNew - O2.y) + O2.y

EDIT: Doing a bit of substitution, this can be simplified (if you like) to:


// Apply the first (outer) rotation
xNew = cos * (x - O.x) - sin * (y - O.y) + O.x
yNew = sin * (x - O.x) + cos * (y - O.y) + O.y
// Apply the second (inner) rotation
xNew2 = cos * (xNew - O.x)/2 - sin * (yNew - O.y)/2 + (xNew + O.x)/2
yNew2 = sin * (xNew - O.x)/2 + cos * (yNew - O.y)/2 + (yNew + O.y)/2

[s]Yes that might work ! but does it works for all the cases?

For example if I want the first (outer) rotation to be far from the sword like O.x = x - 500 and O.y = y
And the second (inner) rotation to, not be in the middle of the sword, but at it’s grip, something like O2.x = x + 40 and O2.y = y + 20

Should I do something like

O2.x = (xNew + O.x) / 2.0 + 40
O2.y = (yNew + O.y) / 2.0 + 20

to place it where I want ?
[/s]
edit: the code in this message is false

Depends on what you mean by all cases. :slight_smile:
I mean, the formula makes an assumption that the center of rotation for the second rotation is always halfway between O and ‘B’, because that was how it looked like in your image. If you were to rotate a point ‘A’ that is not the tip of the sword, then of course also the second rotation center will be different.
And if you choose anoter rotation center for the first/outer rotation, then also everything will be different.
The formula (A + B) / 2 is just used to find the center between A and B.
You can of course compute O2 however you like and make it depend on any other computed value or constant.

Ok I got it I modified the formula a bit to place the other centre points where I want :slight_smile:

Alright that’s it! I can now translate, do multiple rotations with multitple centre points and everything works wonderfully!
Look at how amazing it looks even without any textures, these are just the polygons but it’s so cool already ;D

http://image.noelshack.com/fichiers/2015/34/1439816163-swordswipe.gif

Big thanks to everybody for the help ! :slight_smile:

Looks really nice! At some points it even looks like it’s somehow 3D-ish. :slight_smile:

Looks amazing! Does it work if you rotate the sword in the other direction?

I’m trying to implement this into my game to show every posibilities when it will be ready :slight_smile:

Sounds spiffy. Can’t wait to play the finished game! :smiley:

And here is the swipe system implemented to my game 8) ! Note that it is always active for the show and to make sure that it works for every movements, but in the real game it will be activated only when you are attacking :slight_smile:

a2xhhLXGHTg

It’s amazing to witness someone in the world try hard and learn something.