Cannot find corner object in motor

I am making a mod for a game called Minecraft PE and I am using it for learning. Before I show my code, I want you to know that Y

is the vertical axis and and X

and Z

are horizontal. Here's the code I used:

Math.asin(Math.sin((fPosXBeforeMoved - sPosX) /
Math.sqrt(Math.pow(fPosXBeforeMoved - sPosX, 2) + 
Math.pow(fPosZBeforeMoved - sPosZ, 2))));

      

I haven't used tan

it because sometimes it returns something like a NaN

certain angle. This code gives us the sine of an angle when I explicitly used Math.asin

. angle

- a value between -1 and 1 and it works! I know this works because when I walk past the Z-axis, I was expecting and it switched from negative to positive. However, I thought it should return the radians? I read somewhere that the input is radians, but my input is not radians. I really want to answer the question of how my own code works and how I was supposed to do it! I've been studying trigonometry all day, but I'm really frustrated, so now I'm asking the question, where will I get all my answers from!

Can someone explain how my own code works and how to modify it to get the angle in radians? Is this what I did right? Am I really giving this to radians and just turning it into something like a sinusoidal type?

+3


source to share


1 answer


Okay, let's quickly get to what sin

and asin

. Take a look at this right triangle in the diagram below:

Source: Wikipedia

Looking at the point of A

this right-angled triangle, we see that there is an angle between the line segment AC

and AB

. The connection between this angle sin

lies in the fact that sin

- the ratio of the length of the opposite side to the hypotenuse. In other words:

sin A = opposite / hypotenuse = a / h

      

This means that if you take a / h

, it will be equal to the sin

angle located at the point A

. Thus, to find the actual angle, we will need to apply the inverse sine operator on both sides of this equation. Thus:

A = asin(a / h)

      

For example, if a = 1

and h = 2

in our triangle, the sine of the angle that this right triangle encloses between AC

and AB

is equal to:

sin A = 1 / 2

      

To find the actual angle that is here, we do:

A = asin(1 / 2)

      

Putting that into your calculator, we get 30 degrees. Radians are another way to represent an angle where the following relationship holds:

angle_in_radians = (angle_in_degrees) * (Math.PI / 180.0)

      

Actually I am a little confused about your code because you do asin

and then sin

after. The property between asin

and sin

is:

arcsin

matches asin

. The above equation states that as long as x >= -Math.PI / 2, x <= Math.PI / 2

or x >= -90, x <= 90

degrees, this relationship holds. In your code, the argument inside sin

will definitely be in the range -1 to 1, and this actually makes it easier:

(fPosXBeforeMoved - sPosX) / Math.sqrt(Math.pow(fPosXBeforeMoved - sPosX, 2) + 
Math.pow(fPosZBeforeMoved - sPosZ, 2));

      

If you want to find the angle between the moved points, you do not use the right sides of the triangle. I'll talk about this later.


Ok, so how does this relate to your question? Take a look at the equation you have in your code. We have four points to pay attention to:



  • fPosXBeforeMoved

    - Position of X

    your point before moving
  • sPosX

    - Position of X

    your point after moving
  • fPosZBeforeMoved

    - the position of Z

    your point before moving
  • sPosZ

    - the position of Z

    your point after moving.

We can actually represent it in a right-angled triangle like this (sorry bad diagram):

enter image description here

We can think of a point before you moved (fPosXBeforeMoved,fPosZBeforeMoved)

on a plane XZ

, and a point (sPosX,sPosZ)

when you moved it after. This diagram X

will have the horizontal component and the Z

vertical component. Imagine holding an image in front of you. X

there will be an axis going from left to right, there Z

will be an axis up and down, and there Y

will be an axis going out towards you and going inside the image.

We can find the length of the adjacent segment ( AC

) using the difference between the coordinates X

and the length of the opposite segment ( AB

), taking the difference between the Z

coordinates. All we need is to find the length of the hypotenuse ( h

). If you remember from school, this is simply done using the Pythagorean theorem:

h^2 = a^2 + b^2
h = sqrt(a^2 + b^2)

      

So if you link to a diagram, our hypotenuse is thus (in JavaScript):

Math.sqrt(Math.pow(fPosXBeforeMoved - sPosX, 2) + Math.pow(fPosZBeforeMoved - sPosZ, 2));

      

You will learn this as part of your code. We've covered sin

, but we'll look at cos

. cos

- the ratio of the length of the adjacent side to the hypotenuse. In other words:

cos A = adjacent / hypotenuse = b / h

      

This explains this part:

(sPosX - fPosXBeforeMoved) / Math.sqrt(Math.pow(sPosX - fPosXBeforeMoved, 2) + 
Math.pow(sPosZ - fPosZBeforeMoved, 2));

      

Note that I am replacing subtraction sPosX

and fPosXBeforeMoved

versus what you had in your code. The reason is that when you study a point before and after point, point after always comes first , then point before second. At the bottom, when you compute the hypotenuse, it doesn't matter, because no matter what order the values ​​are subtracted from, we take the square of the subtraction, so you get the same number anyway, no matter the order. I decided to reverse the order of the hypotenuse here to be consistent. The value matters at the top, as a value that is positive or negative when subtracted will matter when you find the angle at the end.

Note that this separation will always be between -1 and 1, so we can of course use inverse trigonometric functions here. Finally, if you want to find the angle, you will apply the inverse cosine . In other words:

Math.acos((sPosX - fPosXBeforeMoved) / Math.sqrt(Math.pow(sPosX - fPosXBeforeMoved, 2)
+ Math.pow(sPosZ - fPosZBeforeMoved, 2)));

      

I believe you need to program. Note that this will return the angle in radians . If you want it to be in degrees, use the equation I showed you above, but rearrange it so that you solve in degrees instead of radians. Thus:

angle_in_degrees = angle_in_radians * (180.0 / Math.PI)

      

As far as what you have now, I suspect that you are simply measuring the ratio of neighbor and hypotenuse, which is perfectly fine if you want to determine where you cross each axis. If you want to find the actual angle, I would use the above code.


Good luck and have fun!

+3


source







All Articles