Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Math problem (trigoniometry)  (Read 2160 times)
0 Members and 1 Guest are viewing this topic.
Offline 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
<B in both triangles (added).



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?
Offline Several Kilo-Bytes

Senior Duke


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?
Offline 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 Sad

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

Senior Duke


Medals: 15



« 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?
Offline Agro
« Reply #4 - Posted 2013-08-24 21:02:47 »

learn some inverse kinematics 8D

Offline 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 Smiley
*Do you have any self explaining article?
Offline 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?
Offline Several Kilo-Bytes

Senior Duke


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.
Offline 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.

"It's after the end of the world! Don't you know that yet?"
Offline 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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Several Kilo-Bytes

Senior Duke


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.)
Offline 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.40037140150904
abc:59.29531617541667
bca: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.

"It's after the end of the world! Don't you know that yet?"
Offline 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.40037140150904
abc:59.29531617541667
bca: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.
Offline philfrei
« Reply #13 - Posted 2013-08-25 20:20:27 »

oops  Clueless [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.

"It's after the end of the world! Don't you know that yet?"
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


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

Apologies for my bad explanations.
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?
Offline 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?

"It's after the end of the world! Don't you know that yet?"
Offline Several Kilo-Bytes

Senior Duke


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.
Offline 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.
Offline 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.

Huh??

"It's after the end of the world! Don't you know that yet?"
Offline 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 Cheesy
Offline 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 Cheesy
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 Cheesy:D
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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (55 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (44 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!