I'm trying to figure out how to calculate the largest radius of the screen's visible region.
(Before you say: Hey! Rectangulars don't have a radius! I understand that screen is rectangular (if camera is positioned in 90 degrees and not tilted), I'm talking about the radius of an imaginary circle that wraps the screen, or, the distance of the edge between the center of the screen to one of the corners of the rectangular, which is the same).
So, I understand that in a normal situation when screen is not tilted, I could take the distance between VisibleRegion.latLngbounds.southwest and the center of the screen to find the radius, but when screen is tilted, it becomes trapezoid.
Now, if the screen is an isosceled trapezoid, then I could take VisibleRegion.farLeft for example (which would be equal to farEast) and calculate the distance this way:
VisibleRegion vr = map.getProjection().getVisibleRegion();
Location center = new Location("center");
center.setLatitude(screenCenter.latitude);
center.setLongitude(screenCenter.longitude);
Location farVisiblePoint = new Location("farPoint");
farVisiblePoint.setLatitude(vr.farLeft.latitude);
farVisiblePoint.setLongitude(vr.farLeft.longitude);
float radius = center.distanceTo(farVisiblePoint);
My question is:
Is there any situation with the map that the calculation above will be wrong? Can the trapezoid not be an isosceled one?
Related
I followed an online tutorial, I did not understand how the professor managed to get the distance of a circle from the top left corner of the screen:
//calculate the distance from epicenter (of a circle) to the top left corner of the screen
double theta = atan(epicenter.dy/epicenter.dx);
double distanceToCorner = epicenter.dy / sin(theta);
I would like to know how to get the distance from all the other screen corners (and possibly have an explanation of what has been done).
Thank you
Assuming, that in android you can get screen width and height, you can simply count the distance at horizontal and vertical axis separately.
Getting the distances at those axes, you can use Pythagoras equation, like
dist = sqrt( dx^2 * dy^2 )
To make it more felxible, just make a function, that takes corner position as a parameter and make the dx and dy as a absolute difference of corner and epicenter location.
Going back to your question and atan(...), I don't quite understand the need of using this here, except if that's a project for the math class :)
I know this is not the answer but if i understand what you mean, then this image might be helpful.
I am making a 3D game in which the player can rotate their view point via the mouse to look around the environment. I firstly just did x and y rotation via vertical and horizontal movement of the mouse and z via another control. But after playing the game I realised it did not rotate correctly. NOTE: I have a global variable matrix which represents the player's angle (3x1), at 0,0,0 it seems to work correctly as up or down is a direct x axis rotation and right or left is a direct y axis rotation, but if I move my camera diagonally for example then left doesn't directly correlate to a y axis rotation anymore.
Visually on a unit circle the players viewpoint wouldn't travel the full circumference anymore and would travel in a circle that is smaller that the circumference. This is the current code (x and yRateOfRot is the ratio of how far away from the centre the cursor is in each direction between -1 and 1):
private static void changeRotation(){
angle.set(Matrix.add(angle.matrix,new double[][]{
{ROTATION_SPEED * camera.xRateOfRot()},
{ROTATION_SPEED * camera.yRateOfRot()},
{ROTATION_SPEED * camera.zRateOfRot()}}));
}
I have looked at this source http://paulbourke.net/geometry/rotate/ and understand how to rotate via an arbitrary axis which I could do but I am not sure how to correlate this into getting a ratio to find out what the x,y and z change would be for looking in a specific direction i.e. at 0,0,0 the ratio of looking up would be x:1, y:0, z:0 but then at another angle the ratios would be different as looking up no longer means only an x rotation. Any information would be appreciated, thanks!
I have a little tech game I am messing around with and I can't figure out the formula to position 1 object given another objects origin.
So I have a Spaceship and a Cannon. I have the game setup to use units, so 1 unit = 16 pixels (pixel art).
Basically my cannon should be placed 0.5625 units on the X and 0 on the Y relative to the origin of the Spaceship, which is located at 0, 0 (bottom left corner).
The cannon should is independent on the angle of the spaceship, it can aim in different directions rather than being fixed to aim the way of the spaceship.
I have it constantly following the cursor, which works fine. Now when I rotate the Spaceship, obviously the origin of the Spaceship is changing in world coordinates, so my formula to place the cannon is all messed up, like so:
protected Vector2 weaponMount = new Vector2();
weaponMount.set(getBody().getPosition().x + 0.5625f, getBody()
.getPosition().y);
Obviously if I position the ship at a 90° angle, X is going to be different and the cannon would be waaaayyy off the ship. Here is a screenshot example of what I mean:
What would be the formula for this? I have tried using cos/sin but that does not work.
Any ideas?
weaponMount.set(0.5625f,0).setAngle(SpaceshipAngle).add(getBody().getPosition());
Where SpaceshipAngle is the angle of your Spaceship.
The origin of the spaceship is the point, arround which the spaceship will rotate and scale (the Texture of it). The position instead is always the lower left corner of the Texture and does not depend on the rotation.
Your problem is, that your offset does not depent on the rotation of your spaceship.
To take care about this rotation you should store a Vector2 offset, which describes your weapons offset (in your case it is a Vector2(0.5625f, 0)).
Next store a float angle describing your spaceships rotation.
Then you can rotate the offset by using: offset.setAngle(rotation).
The last thing is to set the weapons position. The code for this did not change so much:
weaponMount.set(getBody().getPosition().x + offset.x, getBody()
.getPosition().y + offset.y);
Circle c1 = new Circle(20); //Initialize
c1.relocate(200, 200); //Set coordinates
I will say right off the bat that I do not understand trigonometry. From what I've been able to find online, I need to use cos and sin + an angle, but I do not understand how to make it happen.
How do I make the circle rotate around a specific point? I want to have a gap of 5-10 pixels between the rotation point and the circle's layout. So visually, the circle will be rotating around a blank space of 5-10 pixels.
Code snippets will be greatly appreciated, as well as tutorials.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find angle of a point from center of circle
Imagine a circle, the center point is fixed, as is the point at the top of the circle. What i want is to give a third point anywhere around the circle (or outside the circle) and get the degrees from the center to top point line from 0-359. (I actually graphed out a nice picture illustrating but im new and cant post it)
To give some context, the circle is a little planet and I want do place a little building on the surface with the base of the building at a tangent. I need the rotation angle for my building bitmap.
edit: Thanks for the help, i'm still struggling with this one though. I wonder could it be relevant that I'm using android and the y0 coordinate is at the top? Is it the other way around on other platforms? would that affect the calculation?
Solution: Because I am in android and the y coords are counted from top to bottom I had to change a - witha +
degrees = Math.atan2(x - centerX, -y + centerY);
// and to make it count 0-360
if (degrees < 0) {degrees += 2 * Math.PI;}
Use Math.atan2() to get the angle in radians from east, and then rotate and convert as appropriate.
Note that atan2 is defined as atan2(y, x) NOT atan2(x, y), as you might expect.
Get the horizontal distance and the vertical difference between the center and the point, divide one by the other, and pass the result to the method Math.asin(double).
The result will be the angle in radians. When you need it in degree, you can use the method Math.toDegrees(double). Most APIs I know prefer radians, though.