Question

My problem basically is to calculate the x and y coordinates of the second element in the following situation.

Is for a tool in unity3d, using c#

enter image description here

Était-ce utile?

La solution 2

If you know the angle between the x-axis and this box shaped object you can use some basic trig.

Lets assume the box shaped object has a length of "len". ( It would really help to better label what is going on, I don't fully understand.) and lets say you have an angle of θ.

I'm assuming the centre of the circle has coordinates (0,0).

The then the vertical distance from the centre of the circle to the edge of the circle where the box touches is y = len * sin(θ) and for the horizontal distance is len * cos(θ).

If you are only going about 2/5ths of the way up the box thing you would use len / 5 instead of len.

This is just the math behind it. In c# you want to use the Math class. it has all the functions you need. Be careful with radians and degrees.

Autres conseils

So I'm guessing you have the coordinates of A and B. Find the angle of the second line with:

float angle = atan2(B.y-A.y, B.x-A.x)

This only works if your situation is axis-aligned like in your diagram (i.e. if the original configuration is lined up along the x axis). If not, you can solve the formula |U x V| = |U| |V| sin(angle) for angle (you will need an arcsin -- the inverse of sin), where U and V are the old and new AB vectors.

Then rotate your point of interest (call it P) around A. You do this by first subtracting A's coordinates from P so the axis of rotation is at the origin. Then rotate P by multiplying with the rotation matrix:

[ cos(angle)    -sin(angle) ]  [ P.x ]
[ sin(angle)     cos(angle) ]  [ P.y ]

Which gives

x = cos(angle) * P.x - sin(angle) * P.y
y = sin(angle) * P.x + cos(angle) * P.y

After you have these, add A's coordinates back in.

In summary:

P_new = A + rotate(P_old - A)

The actual code will be more involved than this, but this is the abstract picture. I'll leave the coding to you.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top