Math problem (trigoniometry)

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


    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?

Here are formulas you can use to find the angles of a triangle given three sides:
A^2 + B^2 - ABcos(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?

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 :frowning:

Im trying to find the angles to rotate the joints of the bones in the characters arm (bone structure).


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.

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?

learn some inverse kinematics 8D

The model consists of joints (the red dots).

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

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?

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.

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


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.

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?

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

Using the wikipedia link formula for “angle of cosines” solution, when given 3 side lengths:


		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:

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.

oops :clue: [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.

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?

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?

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.

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.

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.

???

Correct

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 :smiley: