Simple Math for controlling characters movement based on camera rotation - c#

I have a GameObject known as myCamera. I am trying to use the Euler Angle of said GameObject to determine in what direction the character should move. An example of the return values expected are located here:
if 0 Degrees x = 1; z = 0
if 90 Degrees x = 0; z = 1
if 180 Degrees x = -1; z = 0;
if 270 Degrees x = 0; z = -1;
The current code looks like this:
// TempVar[0] = tempX; [2] = finalX; [1] = finalZ;
myCameraRotation = myCamera.transform.rotation.eulerAngles;
if (myCameraRotation.y > 180f)
{
tempVar[0] = (int)myCameraRotation.y - 360f;
Debug.Log("Over 180 - New Value: " + tempVar[0]);
}
else
{
tempVar[0] = (int)myCameraRotation.y;
Debug.Log("Under 180 - Passing Value: " + tempVar[0]);
}
if (tempVar[0] > -90 && tempVar[0] <= 90)
{
tempVar[1] = (1 * Mathf.Abs(1 - (tempVar[0] / 90)));
} // Calculate Z
else if (tempVar[0] <= -90 && tempVar[0] > 90)
{
tempVar[1] = ((0 - 1) * Mathf.Abs(1 - (tempVar[0] / 90)));
} // Calculate Z
if (tempVar[0] > 0 && tempVar[0] <= 180)
{
tempVar[2] = (1 * Mathf.Abs(1 - (tempVar[0] / 90) / 2));
} // Calculate X
else if (tempVar[0] <= 0 && tempVar[0] > 180)
{
tempVar[2] = (-1 * Mathf.Abs(1 - (tempVar[0] / 90) / 2));
} // Calculate X
Debug.Log("Z Value : " + tempVar[1]);
Debug.Log("X Value : " + tempVar[2]);
This is outputting some really funky values and I am not really sure what is going on with it, to be honest. As previously stated I am getting the Euler Angle of the myCamera GameObject, not the rotation.
The more I try to work on this the more horrific numbers start popping up, any help from someone who knows some math would be much appreciated.

If the y-rotation is the only rotation of the object, you can use the first column of the object's matrix as the forward direction.
If there are more transforms, you can calculate the direction you want with:
x = Math.Cos(myCameraRotation.y);
z = Math.Sin(myCameraRotation.y);

Related

Is ARC clockwise or counterclockwise In SVG?

I have to draw an arc between two points at the edge of a circle. Assuming that the arc is drawn always in the shortest distance possible(edit2: sweep flag set to 0), I need to know if the arc is drawn clockwise or anticlockwise.
I got the following inputs:
start point (point1)
end point (point2)
center point
radius
Edit1: This question is related to arcs in svg. Hence its belongs to programming.
The answer is SVG supports both clockwise and anticlockwise arcs. You switch between them by changing the "sweep" flag in an arc (A) command.
If you want to learn how SVG arcs work, the place to look is the Paths arcs section of the SVG specification.
Having described data, you can find what arc is smaller - CCW or CW one.
Just check sign of cross product of vectors point1-center and point2-center to reveal shortest arc orientation:
cp = (p1.x-c.x)*(p2.y-c.y)-(p1.y-c.y)*(p2.x-c.x)
For cp>0 you have to set sweep parameter to 0, otherwise to 1 (perhaps vice versa), whilst large-arc-flag should be always 0.
After discussing with a mathematician, I found an answer to this and based on that I wrote the following code:
public static bool IsClockWise(Point point1, Point point2, Circle circle)
{
var dxPoint1 = point1.X - circle.CenterX;
var dyPoint1 = point1.Y - circle.CenterY;
var dxPoint2 = point2.X - circle.CenterX;
var dyPoint2 = point2.Y - circle.CenterY;
double thetaPoint1 = 0, thetaPoint2 = 0;
if (dxPoint1 > 0 && dyPoint1 >= 0)
thetaPoint1 = Math.Asin(dyPoint1 / circle.Radius);
else if (dxPoint1 <= 0 && dyPoint1 > 0)
thetaPoint1 = Math.PI - Math.Asin(dyPoint1 / circle.Radius);
else if (dxPoint1 < 0 && dyPoint1 <= 0)
thetaPoint1 = Math.Abs(Math.Asin(dyPoint1 / circle.Radius)) + Math.PI;
else if (dxPoint1 >= 0 && dyPoint1 < 0)
thetaPoint1 = 2 * Math.PI - Math.Abs(Math.Asin(dyPoint1 / circle.Radius));
if (dxPoint2 > 0 && dyPoint2 >= 0)
thetaPoint2 = Math.Asin(dyPoint2 / circle.Radius);
else if (dxPoint2 <= 0 && dyPoint2 > 0)
thetaPoint2 = Math.PI - Math.Asin(dyPoint2 / circle.Radius);
else if (dxPoint2 < 0 && dyPoint2 <= 0)
thetaPoint2 = Math.Abs(Math.Asin(dyPoint2 / circle.Radius)) + Math.PI;
else if (dxPoint2 >= 0 && dyPoint2 < 0)
thetaPoint2 = 2 * Math.PI - Math.Abs(Math.Asin(dyPoint2 / circle.Radius));
if (thetaPoint1 > thetaPoint2)
{
return !(thetaPoint1 - thetaPoint2 <= Math.PI);
}
return thetaPoint2 - thetaPoint1 <= Math.PI;
}

c# calculater bearing between two lat and long coords from NMEA log file?

With the following extract from a GPS log:
$GPGGA,153500.009,5137.2603,N,00244.8715,W,1,10,0.8,50.6,M,51.4,M,,0000*71
$GPRMC,153500.009,A,5137.2603,N,00244.8715,W,037.7,101.7,300912,,,A*74
$GPGGA,153500.059,5137.2601,N,00244.8706,W,1,10,0.8,50.6,M,51.4,M,,0000*74
$GPRMC,153500.059,A,5137.2601,N,00244.8706,W,038.0,101.8,300912,,,A*76
$GPGGA,153500.109,5137.2600,N,00244.8697,W,1,10,0.8,50.6,M,51.4,M,,0000*78
$GPRMC,153500.109,A,5137.2600,N,00244.8697,W,038.3,101.9,300912,,,A*78
$GPGGA,153500.159,5137.2599,N,00244.8688,W,1,10,0.8,50.5,M,51.4,M,,0000*73
$GPRMC,153500.159,A,5137.2599,N,00244.8688,W,038.6,101.9,300912,,,A*75
$GPGGA,153500.209,5137.2597,N,00244.8679,W,1,10,0.8,50.5,M,51.4,M,,0000*75
$GPRMC,153500.209,A,5137.2597,N,00244.8679,W,038.9,102.0,300912,,,A*76
I am comparing the logged GPS bearing with a calculated bearing between the last and current position with the following code that loops through each line:
string[] splitline = line.Split(',');
course = Convert.ToDouble(splitline[8]);
Lat = Convert.ToDouble(splitline[3]);
Long = Convert.ToDouble(splitline[5]);
LatDeg = (Convert.ToInt16(Lat) / 100) + (Lat - (Convert.ToInt16(Lat) / 100) * 100) / 60;
LongDeg = (Convert.ToInt16(Long) / 100) + (Long - (Convert.ToInt16(Long) / 100) * 100) / 60;
lastLatDeg = (Convert.ToInt16(lastLat) / 100) + (lastLat - (Convert.ToInt16(lastLat) / 100) * 100) / 60;
lastLongDeg = (Convert.ToInt16(lastLong) / 100) + (lastLong - (Convert.ToInt16(lastLong) / 100) * 100) / 60;
var dLon = lastLongDeg - LongDeg;
var y = Math.Sin(dLon) * Math.Cos(lastLatDeg);
var x = Math.Cos(lastLatDeg) * Math.Sin(LatDeg) - Math.Sin(lastLatDeg) * Math.Cos(LatDeg) * Math.Cos(dLon);
Console.WriteLine(DEG_PER_RAD * Math.Atan2(y, x));
Console.WriteLine("> " + course + " <");
lastLat = Lat;
lastLong = Long;
lastcourse = course;
results in the following:
136.131182151555
> 101.8 <
117.480364881602
> 101.9 <
117.480186101881
> 101.9 <
136.130309531745
> 102 <
117.479649572813
> 102 <
are my calculations out as none of them seem to come close to the gps logged bearing of around 101 degrees?
Thanks
There are a few problems I spotted in the code, for a start when interpreting the latitude and longitude you should look at which quadrant of the earth the positions fall into and convert to negative for south or west locations:
Lat = Convert.ToDouble(splitline[3]);
if (splitline[4] == "S")
Lat = 0.0 - Lat;
Long = Convert.ToDouble(splitline[5]);
if (splitline[6] == "W")
Long = 0.0 - Long;
The remainder of the problems stemmed from passing degrees rather than radians to the math functions and the calculation of the longitude delta seemed reversed. I introduced a few helper functions and rewrote that section of code as follows:
public static double DegreesToRadians(double degrees)
{
return degrees * (Math.PI / 180);
}
public static double RadiansToDegrees(double radians)
{
return radians * 180 / Math.PI;
}
double dLon = DegreesToRadians(LongDeg - lastLongDeg);
double y = Math.Sin(dLon) * Math.Cos(DegreesToRadians(lastLatDeg));
double x = Math.Cos(DegreesToRadians(lastLatDeg)) * Math.Sin(DegreesToRadians(LatDeg)) - Math.Sin(DegreesToRadians(lastLatDeg)) * Math.Cos(DegreesToRadians(LatDeg)) * Math.Cos(dLon);
Console.WriteLine((RadiansToDegrees(Math.Atan2(y, x)) + 360.0) % 360);
Console.WriteLine("> " + course + " <");
That gave me the following results with your test data ignoring the first invalid one where the bearing has not yet been determined:
109.693614586392
> 101.8 <
100.14641169874
> 101.9 <
100.146411372034
> 101.9 <
109.693611985053
> 102 <
I noticed from the GGA speeds that the unit seems to have been either stationary or moving very slowly. Some GPS receivers will filter or hold heading information under those circumstances so some variation can be expected. After the changes I ran through some GPS data I had from a moving vehicle and the results were within one degree of each other.

Moving rectangle is not moving according to coordinates given

I have a picturebox with a picture as a background (infact a map), and on it I am spawning rectangles. These rectangles are supposed to move by given points. The rectangle is moving with assigned speed, and after reaching (or getting close) to one of the points, it starts moving to the next one. My problem however is, the rectangle doesnt move directly to the given point, it is just proceeding to get close to only one of the coordinates, so there are situations where the Y coordinate of the rectangle, and the Y coordinate of the point are the same, but the rectangle is like 60 pixels of and wont move.
Below I am adding a picture as an example of the movement. Blue is expected route, red is the actual one. I have checked the coordinates like a hundred times, they are correct, the rectangle is just moving elsewhere. Note: this only happens at some of the points.
Here is the code I am using to count the movement of the rectangle relative to axis X and Y.
public void Move_calculate(Graphics g)
{
if (points[passed].X == 0 || points[passed].Y == 0) // this happens when the rectangle reaches it final point - it stays where it is (working fine)
{
Redraw(g);
return;
}
speed = randomNumbers.Next(7, 13);
if (points[passed].X > x_coordinate && points[passed].Y > y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = Bx - x_coordinate; // x_coordinate is the x coordinate of the rectangle
distanceForAlfaY = By - y_coordinate;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5; // slowing down when approaching the point
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX); // tangent alpha
x_change = (int)(speed * (Math.Cos(alpha))); // get distance moved relative to axis X
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change))); // again distance for axis Y, using Pythagoras theorem
x_coordinate += x_change;
y_coordinate += y_change;
}
else if (points[passed].X > x_coordinate && points[passed].Y < y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = Bx - x_coordinate;
distanceForAlfaY = y_coordinate - By;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate += x_change;
y_coordinate -= y_change;
}
else if (points[passed].X < x_coordinate && points[passed].Y > y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = x_coordinate - Bx;
distanceForAlfaY = By - y_coordinate;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX+distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate -= x_change;
y_coordinate += y_change;
}
else if (points[passed].X < x_coordinate && points[passed].Y < y_coordinate)
{
Bx = points[passed].X;
By = points[passed].Y;
distanceForAlfaX = x_coordinate - Bx;
distanceForAlfaY = y_coordinate - By;
if (distanceForAlfaX <= 20 || distanceForAlfaY <= 20) speed = 5;
if (distanceForAlfaX + distanceForAlfaY <= 15) passed += 1;
alpha = (distanceForAlfaY / distanceForAlfaX);
x_change = (int)(speed * (Math.Cos(alpha)));
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
x_coordinate -= x_change;
y_coordinate -= y_change;
}
else
{
MessageBox.Show("Something went wrong"); // just notify me that it isnt working again..
}
Pen p = new Pen(Color.Turquoise, 2);
r = new Rectangle(x_coordinate, y_coordinate, 5, 5); // redraw the rectangle
g.DrawRectangle(p, r);
p.Dispose();
}
I have no idea why this is happening, could anyone help with this?
P.S.
There is absolutely no need for the movement to be smooth, the positions of rectangles will be updated once per two seconds using a Timer. For now it is temporarily set to a button.
EDIT:
Here is the foreach code. The labels are just the coordinates shown next to the PictureBox
foreach (aircraft acft in aircrafts) // aircraft is an array aircrafts[]
{
label2.Text = "xp" + acft.points[acft.passed].X;
label3.Text = "yp" + acft.points[acft.passed].Y;
label4.Text = acft.passed.ToString();
label5.Text = "y" + acft.y_coordinate.ToString();
//MessageBox.Show(acft.points[0].X.ToString());
acft.Move_calculate(e.Graphics);
spawn = string.Empty;
}
EDIT2: All variables in aircraft class
public string callsign;
public int speed;
public double heading;
public bool moving = false;
public Point[] points;
public double alpha;
public int x_change;
public int y_change;
public int x_coordinate;
public int y_coordinate;
public int Bx;
public int By;
public double distanceForAlfaX;
public double distanceForAlfaY;
public int passed = 0;
public Rectangle r;
I guess, there's a math mistake in
y_change = (int)Math.Sqrt(((speed * speed) + (x_change * x_change)));
Moreover...
// again distance for axis Y, using Pythagoras theorem
Let Mr.Pythagoras be, I would rather use the same as for X axis
y_change = (int)(speed * (Math.Sin(alpha)));
Have you checked your coordinate system? (https://web.archive.org/web/20140710074441/http://bobpowell.net/coordinatesystems.aspx)
Sorry, wrong link for your issue. Try debugging using Control.PointToClient to make sure all coordinates are expressed in client space. (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.pointtoclient)
You could try:
Point cPoint = this.PointToClient(new Point(x_coordinate, y_coordinate));
Size cSize = new Size(5,5);
r = new Rectangle(cPoint, cSize); // redraw the rectangle
g.DrawRectangle(p, r);
Could you post the data types of your variables to clarify your code? You might be losing precision somewhere, especially if you're using integer division.
(Note for DJ KRAZE, a -= b; in C# can also mean a = a - b; Context matters.)

How to calculate turning direction

I have a three lat-lon coordinates that make up two line segment A to B to C. I also found a function that can return north-bearing of a line segment A-B or B-C in -180 to 180 manner. However, I'm having trouble to determine when a car reaches from A to B, should it turn right or left to continue to C.
EDIT: Previous answer was wrong. now this is the correct
public Direction GetDirection(Point a, Point b, Point c)
{
double theta1 = GetAngle(a, b);
double theta2 = GetAngle(b, c);
double delta = NormalizeAngle(theta2 - theta1);
if ( delta == 0 )
return Direction.Straight;
else if ( delta == Math.PI )
return Direction.Backwards;
else if ( delta < Math.PI )
return Direction.Left;
else return Direction.Right;
}
private Double GetAngle(Point p1, Point p2)
{
Double angleFromXAxis = Math.Atan ((p2.Y - p1.Y ) / (p2.X - p1.X ) ); // where y = m * x + K
return p2.X - p1.X < 0 ? m + Math.PI : m ); // The will go to the correct Quadrant
}
private Double NormalizeAngle(Double angle)
{
return angle < 0 ? angle + 2 * Math.PI : angle; //This will make sure angle is [0..2PI]
}
Edited to fix the over 180 issue, also now supports U-Turns.
const int THRESHOLD = 0;
Direction TurnLeftOrRight(Point A, Point B, Point C)
{
int angle = ToAngle(B,C) - ToAngle(A,B);
if((angle > THRESHOLD && angle < 180 - THREASHOLD) || angle < -180 - THREASHOLD)
return Direction.Right;
else if ((angle < 0 - THREASHOLD && angle > -180 + THREASHOLD) || angle > 180 + THREASHOLD)
return Direction.Left;
else if (angle >= 0 - THREASHOLD && angle <= THREASHOLD)
return Direction.Straight
else
return Direction.UTurn;
}
You could also do tolerances between left right and strait just change the first angle > 0 to angle > 45 and the second one to angle < -45
I think your life will be simpler if you use the vector cross product.
Although strictly speaking cross product is defined only for 3D vectors, for 2D vectors p=(px,py) and q=(qx,qy), you can think of their cross product p×q as as pxqy - pyqx. This latter number will be positive if p is clockwise from q, and negative if p is counter-clockwise from q. It will be zero if p and q are parallel - i.e. point in the same or opposite directions.
In your case, you are using (lat,lon). The equivalent in (x,y) coordinates is (-lon, lat), so if you have two vectors (lat1,lon1) and (lat2,lon2), you want to calculate (-lon1, lat1)×(-lon2, lat2), which comes out to lat1*lon2-lon1*lat2.
If this number is zero, you can use the dot product to tell whether the direction is straight or a U-turn.
So your code could look like this, assuming Points and Vectors are written in (lat, lon) form (the code would be a little different if they are in x and y):
public Direction GetTurnDirection(Point A, Point B, Point C)
{
Vector v1 = B - A ;
Vector v2 = C - B ;
double cross = v1.lat*v2.lon - v1.lon*v2.lat ;
if (cross > 0) { return Direction.Left ; }
if (cross < 0) { return Direction.Right ; }
double dot = v1.lat*v2.lat + v1.lon*v2.lon ;
if (dot > 0) { return Direction.Straight ; }
return Direction.UTurn ;
}
If AB is the bearing of B from A, and BC that of C from B, the
turn angle is remainder( BC-AB, 360.0); (assuming degrees). If this
is positive the turn is to the right. In your example remainder( BC-AB, 360.0)
is remainder(271,360) = -89.

Pick X points on a circle which are on screen in C#

I have a few sets of information available to me, currently:
position on screen (Point)
orientation of point (double, say 15 degrees)
distance (length from point)
What I want to do is pick X (less than 6 I think) number of points in an angular spread facing the same orientation as the position with a distance of distance so that they are always on screen.
I can do this, but my implementation does not take into account the fact that some of the points end up off the screen, and all my attempts are quite poor and overly complex.
Does anyone have a good way of doing this?
EDIT: Added graphic to show what im after...
A simple way of "projecting" a point (X1, Y1) along a trajectory/orientation for a distance to X2, Y2 you can use the following;
X2 = X1 + (int)(Math.Sin(orientation) * distance);
Y2 = Y1 + (int)(Math.Cos(orientation) * distance);
Where orientation is a radian double value. You may want to round the result since (int) is a brutal way to convert to int.
If you want to pick a point X/Y that is atleast distance d from point pX, pY then you know that the hypotenuse ( SquareRoot ( (X-pX)^2 + (Y-pY)^2 ) is less than d^2.
Is X/Y less than d from pX/pY?
bool isTooClose = ( ((X - pY)*(X - pY)) + ((Y - pY)*(Y - pY)) < (d*d));
If you know the screen size, then just check the boundaries.
bool isOnScreen = ( (pY > 0) && (pX > 0) && (pX < Screen.Width) && (pY < Screen.Height));
If you want to know that a circle is completely on screen, use the above isOnScreen and subtract/add the radius of the circle to the boundary. For example
bool isCircleOnScreen = ( (pY > r) && (pX > r) && (pX < (Screen.Width - r)) && (pY < (Screen.Height - r)));
To pick a point (X2, Y2) on a circle you can use the formula at the top.
This ended up being my implementation:
private static int[] DetermineTagPointsRange(Point location, double orientation, float distance)
{
const int screenWidth = 1024;
const int screenHieght = 768;
const int sampleCount = 10;
const int totalAngleSpread = 360;
const int angleInc = totalAngleSpread / sampleCount;
var lowestAngle = -1;
var highestAngle = -1;
for (var i = 0; i < sampleCount; i++)
{
var thisAngle = angleInc*i;
var endPointX = location.X + (int)(Math.Sin(ToRad(thisAngle)) * distance);
var endPointY = location.Y - (int)(Math.Cos(ToRad(thisAngle)) * distance);
bool isOnScreen = ((endPointY > 0) && (endPointX > 0) && (endPointX < screenWidth) && (endPointY < screenHieght));
if (isOnScreen)
{
if (thisAngle <= lowestAngle || lowestAngle == -1)
lowestAngle = thisAngle;
if (thisAngle >= highestAngle || highestAngle == -1)
highestAngle = thisAngle;
}
}
return new int[] {lowestAngle, highestAngle};
}
Thanks for the help

Categories

Resources