Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Math problem (trigoniometry)  (Read 2879 times) 0 Members and 1 Guest are viewing this topic.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Posted 2013-08-24 19:25:07 »

Im trying to calculate the arms of the character to an certain position.
However im already stuck with the 2d calculation.

I need:
<A in ABD

I have come up wih this code so far, but my math knowledge is really limited (all examples explain other aspects, mostly rectangles with 90 degree corners).
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23 `    public void setLeftArm(float x, float y, float z){        float xdist = x - leftuparm.getParentJoint().getPos().x;        float ydist = y - leftuparm.getParentJoint().getPos().y;        float zdist = z - leftuparm.getParentJoint().getPos().z;                float a = leftdownarm.getHeight();        float b = (float)Math.sqrt(zdist * zdist + ydist * ydist);        float c = leftuparm.getHeight();        float d = 100;        float e = ?;                float uparm = (float)Math.acos((d*d + c*c - e*e)/(2*d*c));         //(b2 + c2 - a2)/2bc                float downarm = (float)Math.acos((c*c + a*a - b*b)/(2*a*c));       //(c2 + a2 - b2)/2ca        //Still need to add second corner from (ABD)        System.out.println("------------------------------");        System.out.println("Dist: " + xdist + " x " + ydist + " x " + zdist);        System.out.println("Rect 1: " + a + " x " + b + " x " + c);        System.out.println("Rect 2: " + c + " x " + d + " x " + e);        System.out.println("Angles: " + FastMath.RadtoDeg(up) + " x " + FastMath.RadtoDeg(down));        System.out.println("------------------------------");}`

In the future i also need to rotate taking the Z in account, i really have no clue how this is going to work.
Can anyone explain how i can get the needed rotations in 2D or even 3D space?
Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #1 - Posted 2013-08-24 20:13:05 »

Here are formulas you can use to find the angles of a triangle given three sides:
A^2 + B^2 - A*B*cos(angle ab) = C^2
sin(angle a) / A = sin(angle b) / B = sin(angle c) / C

I can't infer what you are trying to do, but I noticed you are not using complex numbers or matrices. Is there a reason why you use angles?
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #2 - Posted 2013-08-24 20:56:50 »

My biggest problem at the moment is calculating line BD (cant figure out its length).
Im aware of the cosinus rule, but i dont have all sides of th second rectanglr (ABD), so i cant use it

Im trying to find the angles to rotate the joints of the bones in the characters arm (bone structure).
 1  2 `leftuparm.RotateX(up);leftdownarm.RotateX(down);`

Reason i dont use complex numbers and matrices is because i cant get that far with math.
Using simple trigoniomerty i came this far, but any changes are appriciated.
DrZoidberg

Senior Devvie

Medals: 17

 « Reply #3 - Posted 2013-08-24 21:02:03 »

So you have a coordinate x,y,z and you want the arm to point towards that position?
In which form do you need the angles? What library do you use? Can't you give it the direction in another way?
Agro
 « Reply #4 - Posted 2013-08-24 21:02:47 »

learn some inverse kinematics 8D

RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #5 - Posted 2013-08-24 21:06:27 »

The model consists of joints (the red dots).

Thats indeed what im trying to do, thank you
*Do you have any self explaining article?
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #6 - Posted 2013-08-24 21:25:17 »

I cant find any implementation of (reverse) kinematics, mostly it says i have to solve it using simple trigoniomerty.
So could anyone help me with calculating line BD?
Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #7 - Posted 2013-08-24 22:25:24 »

The only "simple" trig you need to know is the names of relationships on a right triangle (sine, cosine, tangent, cotangent, secant, cosecant, and their inverses) (just the names), the two equations I wrote (which are for non-right triangles), and how to convert Cartesian to polar coordinates. There is no simple or complicated trig. That is the entirety of it.

You need to know an angle of abd. C is the side opposite angle c. Let c be that angle and C be the side opposite it (72, 100, or bd). A and B are the other two sides. You have one unknown, so that is the only equation you need to solve.

If you know only two sides and none of the angles, the third side of the triangle could be any length greater than 72 + 100, so it is impossible to solve for the unknown side length.
philfrei
 « Reply #8 - Posted 2013-08-25 05:01:28 »

@RobinB -

I don't think you have enough info in your diagram to determine length BD. It seems to me BD could be almost any length, based on different possible angles BAD.

Knowing the internal angles of ABC doesn't really help, as according to your figure, the angles in ADB are independent of ABC.

I was looking at the wikipedia post.
http://en.wikipedia.org/wiki/Solution_of_triangles
And found a way to get the internal angles of ABC there. ("Three Sides Given") But that was when I realized, ADB is independent and that this doesn't help.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #9 - Posted 2013-08-25 07:56:41 »

Thank you both, im really bad at this.

Line AB is the upper arm, do you have any idea how i can get the needed rotation?
Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #10 - Posted 2013-08-25 17:00:13 »

All I see are triangles. I do not know what direction/distance each line is supposed to go. Speaking of which, vectors are a means of encoding direction and distance into the same number for any number of dimensions and there are even fewer operations to learn than in trig. (Dot product, cross product, addition, scalar multiplication, normalization, and matrix multiplication.)
philfrei
 « Reply #11 - Posted 2013-08-25 17:11:33 »

Using the wikipedia link formula for "angle of cosines" solution, when given 3 side lengths:
http://en.wikipedia.org/wiki/Solution_of_triangles

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `      double ab = 72;  // distance point a to point b      double bc = 53;  // distance point b to point c      double ca = 64;  // distance point c to point a            // angles, in Radians      // internal angle at point A      double cab = Math.acos( ( (ab * ab) + (ca * ca) - (bc * bc))             / (2 * ab * ca) );      // internal angle at point B      double abc = Math.acos( ( (ab * ab) + (bc * bc) - (ca * ca))             / (2 * ab * bc) );         // internal angle at point C      double bca = Math.PI - (cab + abc);            // output in Degrees      System.out.println("cab:" + cab * 180 / Math.PI);      System.out.println("abc:" + abc * 180 / Math.PI);      System.out.println("bca:" + bca * 180 / Math.PI);`

Runs to give:
 1  2  3 `cab:45.40037140150904abc:59.29531617541667bca:75.30431242307428`

I should mention, I just hacked this out from the wikipedia solution, and have no idea if it is the most practical or efficient for your situation.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #12 - Posted 2013-08-25 19:33:47 »

Using the wikipedia link formula for "angle of cosines" solution, when given 3 side lengths:
http://en.wikipedia.org/wiki/Solution_of_triangles

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `      double ab = 72;  // distance point a to point b      double bc = 53;  // distance point b to point c      double ca = 64;  // distance point c to point a            // angles, in Radians      // internal angle at point A      double cab = Math.acos( ( (ab * ab) + (ca * ca) - (bc * bc))             / (2 * ab * ca) );      // internal angle at point B      double abc = Math.acos( ( (ab * ab) + (bc * bc) - (ca * ca))             / (2 * ab * bc) );         // internal angle at point C      double bca = Math.PI - (cab + abc);            // output in Degrees      System.out.println("cab:" + cab * 180 / Math.PI);      System.out.println("abc:" + abc * 180 / Math.PI);      System.out.println("bca:" + bca * 180 / Math.PI);`

Runs to give:
 1  2  3 `cab:45.40037140150904abc:59.29531617541667bca:75.30431242307428`

I should mention, I just hacked this out from the wikipedia solution, and have no idea if it is the most practical or efficient for your situation.

Thank you, but this is the same code as i have above.
The problem is that corner a does not rotate the upper arm like it should.

The only usable angle in this rectangle is B, because this represents the rotation of the lower arm to the upper arm.
I need to find the angle beteen the arm in default position (line AD) and the arm in current position (line AB), so i can rotate it accordingly.
philfrei
 « Reply #13 - Posted 2013-08-25 20:20:27 »

oops  [re: same solution]

From you diagram, is AD always vertical? That might help.

Do you have the [x, y] of two of the three points A, B, C? Otherwise, it seems the top triangle could be rotated to almost any angle, so there would be no way to know the angle at which AB veers off of the vertical.

Also, angle B (internal to ABC) has no bearing on angle A or B (internal to (ABD). If both angle A (internal to ABD) and line BD are unknown, there are an infinite number of solutions, as changing one will change the other. More info needs to be provided.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #14 - Posted 2013-08-25 21:12:18 »

The line AD should indeed be vertical (so i have a refrence point to rotate from).

The following image shows what im trying:

Point c is known (position of the hand).
Point a is also known (torso base position)
Line AB = 72
Line BC = 53
I want to rotate line AB and CD so the hand will be placed at the position of c.

I figured i should make 2 rectangles and calculate the needed angles, but this seems to be kinda hard.
How can i find out at what angle i should rotate these lines so the hand will be placed at point c?
philfrei
 « Reply #15 - Posted 2013-08-25 21:49:30 »

Given location A and C, you can easily get length AC.

Given lengths AC, AB, BC, you can get the internal angles A, B and C using the earlier formula.

Given locations A, C you can also get the angle or slope of AC off of the vertical, and then subtract the internal angle A from the previous step to get the angle (slope) of the line from A to B.

Yes?

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #16 - Posted 2013-08-25 21:59:27 »

You know the length of three sides. That is enough to find all angles of abc using the law of sines. The position b can be found if you know a and c if you simply find the direction of ac, add a, and extend 72 units in that direction.
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #17 - Posted 2013-08-26 03:50:15 »

The problem is that angle a and c are useless for this problem.
i need to find out how i should rotate a, to point line AB it to b.
Thats why i made an external rectangle abd with a refrence point of 0 degrees.

With no refrence point i am not able to rotate the arm properly.
Think what happens when i rotate the upper arm the same as angle a, it will not be in the position AB, what im really trying to.
philfrei
 « Reply #18 - Posted 2013-08-26 04:00:32 »

I guess I don't understand the problem then.

The rotation needed for AB, you are describing the angle that is formed by DA and AB. Yes?

One way to get that angle is to subtract the inner angle A (AB to AC) from the angle formed by DA and AC. This IS possible because you can get the three lengths and then the inner angle A. AND you have the angle DA to AC because you know the location of A and C.

??

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #19 - Posted 2013-08-26 08:00:31 »

The rotation needed for AB, you are describing the angle that is formed by DA and AB. Yes?
Correct

AND you have the angle DA to AC because you know the location of A and C.
Thanks did not think of that, so i only need to figure out line CD then.
Im going to try this stuff, thanks so much
RobinB

JGO Ninja

Medals: 44
Projects: 1
Exp: 3 years

Spacegame in progress

 « Reply #20 - Posted 2013-08-26 09:45:17 »

OMG it worked, finally my days of frustration are over
I only needed to use rectangle ACD to calculate the position of the upper arm :O
Here and there some tweaking, and now its perfect.

Thank you so much for bearing with me, im so happy :D
Pages: [1]
 ignore  |  Print

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

 MrMapcom (17 views) 2015-05-23 20:26:16 MrMapcom (22 views) 2015-05-23 20:23:34 Waterwolf (31 views) 2015-05-20 15:01:45 chrislo27 (37 views) 2015-05-20 03:42:21 BurntPizza (72 views) 2015-05-10 15:53:18 FrozenShade (56 views) 2015-05-07 09:11:21 TheLopais (220 views) 2015-05-06 13:36:48 TheLopais (203 views) 2015-05-06 13:35:14 TheLopais (208 views) 2015-05-06 13:33:39 TheLopais (228 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x Drenius 13x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x princec 10x KevinWorkman 9x scanevaro 8x orangepascal 8x opiop65 7x Slyth2727 6x revers 6x
 List of Learning Resources2015-05-05 10:20:32How 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:00
 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