I was wondering about this, If I have two points(x,y), where point1.x - point2.x gives its distance between x and point1.y - point2.y gives its distance between y.
Thus we can find the magnitude of those two.
My question is that, I have a bar in between those two point, where the bar’s pivot point is the distance x’s midpoint.
If point1 and point2 only moves up and down, how do I find the bar’s rotation to sync with the point1 and point2’s y movement.
For those of you who have hard time understanding my question, I also attached a pic.
For convenience, use Vector3 values instead of Vector2 values (the reason will become clear in a minute). Do this by just setting all z values to zero.
Here is your data set:
P1a = the location of point 1 before rotation
P2a = the location of point 2 before rotation
P1b = the location of point 1 after rotation
P2b = the location of point 2 after rotation
And the math is…
FirstVector = P1a - P2a; // This is the dark black line in image A
SecondVector = P1b - P2b; // This is the dark black line in image B
AngleBetweenThem = Vector3.Angle(FirstVector, SecondVector);
Done! The reason you should use Vector3 values is solely so you can access the Angle method above as it’s not present for Vector2 values.
Vector3.Angle is there as a convenience method to “hide” that stuff from you if you don’t want to deal with it. If you really have a desire for that sort of thing then I’m going to leave it up to someone else to lay it all out as I’m time sensitive today. Suffice it to say this is standard trig and I’m sure you can easily Google it and find all you ever cared to read on the subject…
I see now, it’s basically figure the angle between 2 vectors using dot product. However, I wanted to use 2 point(1 vector) with midpoint to compute the angle.
Thus I don’t want to store the first vector(from last frame).
The problem here is not only computing the rotation of the midpoint, because if both x and y point moves down, the midpoint.y have to be transformed before the midpoint(pivot point) can be rotated…
Ok,so I wanted to put this into real world/in game problem perspective, currently I am using wheel colliders, and I can compute the offset of the wheels(from the dot product of the point it hits the ground and it’s orginal wheel position).
With the offset of the front wheels I wanted to compute the transformation of y and rotation of the bar.
I am still having hard time to see how much my bar offset in y direction though by just using offset.y.
see picture below see what I mean, so basically the bar between the wheel will be transformed on y direction, then rotate accordingly.
With that been said, by having offset of both point a and b, would I still need to store any points? I figure the midpoint offset is just (offset1+offset2)/2
which allow me to figure out midpoint position which will also allow me to figure out angle.
Why in this case is your midpoint position any sort of a variable? In your car example is your front axle somehow of constantly changing length? I would expect not so you should know where that midpoint is through a simple calculation using the wheel contact points.
So you know:
Where were the wheels contacting before, thus if desired you can calculate where the axle midpoint was before.
Where the wheels are contacting now, thus if desired you can calculate where the axle midpoint is now.
Using the above, plus some baseline data for a neutral “flat” state, calculate all the axle roll information you’d like.
Am I right in thinking you need to get the position and rotation of the bar within the car’s coordinate space (to calculate the tension in the springs)?
First, the axle midpoint is the average of the left wheel position and the right wheel position, both in world coordinates:-
var worldMidPoint: Vector3 = (right.position + left.position) * 0.5;
var carMidPoint: Vector3 = transform.InverseTransformPoint(worldMidPoint);
The Y coordinate of this point is the “height” of the axle in car space. For the torsion, start by getting a vector pointing from the right wheel to the left:-
var axleVec = left.position - right.position;
You can get the “upward” direction of the axle in world coordinates using the cross product of the axle vector and the car’s forward direction:-
var axleUp: Vector3 = Vector3.Cross(axle, transform.forward);
You can now get the angle between the axle’s up direction and the car’s using Vector3.Angle (if you use dot product for this you need to normalise the up vector, and remember that it gives the cosine of the angle, not the angle itself):-
var axleAngle: float = Vector3.Angle(axleUp, transform.up);
If I’ve misunderstood what you are doing here, then post again