Java has a really non-intuitive coordinate scale where the origin (0,0) is at the top left and the Y coordinate increases downwards.
Normally graphs have (0, 0) at the bottom left and the Y coordinate increases upwards.
This makes drawing points and shapes in relation to each other in a graph very difficult.
How can I change it so that the origin is at (0, 0) and Y goes upward?
Thanks
This is not "non-intuitive" if you study display hardware since the screen coordinates also begin in the upper left quadrant.
The solution is to separate your model from your view a la Model-View-Controller, to have your model's coordinates oriented as you see fit, and to write methods that translate model to view and back again where needed.
Related
I am using a GoPro HERO 4 on a drone to capture images that need to be georeferenced. Ideally I need coordinates of the captured image's corners relative to the drone.
I have the camera's:
Altitude
Horizontal and vertical field of view
Rotation in all 3 axes
I have found a couple of solutions but I can't quite translate them for my purposes. The closest one I found is here https://photo.stackexchange.com/questions/56596/how-do-i-calculate-the-ground-footprint-of-an-aerial-camera but I can't figure out how and if it's possible for me to use it. Particularly when I have to take both pitch and roll into account.
Thanks for any help I get.
Edit: I code my software in Java.
If you have rotations in all three axes then you can use these matrices - http://planning.cs.uiuc.edu/node102.html - to construct a full (3x3) rotation matrix for your camera.
Assuming that, when the rotation matrix is an identity (i.e. in the camera's frame) you have defined the camera's axes to be:
X axis for front
Y for side (left)
Z for up
In the camera frame, the rays have directions:
Calculate these directions and rotate them using the matrix to get the real-world axes. Use the camera's real world coordinate as the source.
To calculate the points on the ground: https://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld017.htm
Firstly, apologies for the long title; I could not think of a way to sufficiently cover my problem yet also compact it further.
I am in the process of creating a 2D game in Java with a character in the centre of the screen. My character has a collision box and I have images on the map (looking from above) which also have collision boxes.
However, I'd like to be able to have a "slide" situation if a collision occurs into a side that is not perpendicular to my direction.
For example, if my character is moving east, and there is a wall to his east going in the southwest to northeast direction, instead of just noting that there is an object to the east and not moving, I'd like my character to be able to "slide" along the wall, moving northeast to achieve the attempted Eastern movement.
Is there any easy way to do this?
If it helps in any way, I am using Java Slick2D, and have the collision box of the character as a rectangle where he stands, and the collision boxes of other objects as polygons.
Thanks in advance for any replies!
When you have solved the problem of identifying when a collision has occurred, the sliding motion can be achieved by calculating the component of the motion that is perpendicular to the wall and subtract that from your motion vector.
Say you have a motion vector v = (v_x, v_y) and the normal of the wall n = (n_x,n_y). The normal vector should be of length 1.
Then your new motion vector should be
v_new = v - (v * n) * n.
That in X and Y separately is
v_new_x = v_x - (v_x * n_x + v_y * n_y) * n_y,
and the same way for Y
Class java.awt.geom.Area has a method intersects( Area a ) which should be useful for determining the "point" of impact, call it P. If the character's collision box is a rectangle and the obstacle is bounded by an arbitrary polygon you'll have to determine
the point or side of the rectangle participating in the collision
the point or side of the polygon participating in the collision
If side meets side (they should be parallel), the charcter can't move any more in the current direction; no slide should occur. Equally, if a rectangle side meets a polygon point, movement is blocked.
If a point meets a point, you might resolve this by (arbitrarily?) select one of the sides of the polygon and handle the situation based on that.
Finally, if a rectangle corner meets a slanting side, you'll have to analyze the geometry of this side. (Verify that a 10-to-4 side is met by the upper right (lower left) hand corner if going north or east (south or west), or a 7-to-1 side is met by the upper left (lower right) hand corner when going north or west (south or east)). Then you can determine the forced path along the slanted side.
While this sounds relatively simple, there may be much more complex situations lurking literally round the corner if the collision boxes of objects - polygons - may have concave sections.
so I'm pretty new with opengl and creating 3d shapes. So for my example I have two squares, one with a height/width 2 with the center at the origin coordinate (0,0,-10), and one that is to the far left side of the window. I am trying to rotate the square that lies in the origin along the x-z plane without rotating the square that is located to the far left side of the screen. My approach to this was to save each xyz coordinate of the center square to a variable, and creating a method that uses the behavior of cos(theta) to rotate the square along the x-z plane. My code works, but I assume this is a horrible approach as there must be some more efficient method that is already created that can do the same functionality. I looked at glRotatef(), but from what I understood this only rotates my camera view which in the end would rotate both the middle square and the far left square whereas I only want to rotate the middle square. Is there some other method that already exists that can easily rotate a single 2d shape in 3d space?
In case its relevant, I have included the rotating code I made myself for the middle square: (btw the blue class is just some class I made that has the squares coordinates and the circle degree for cos(theta))
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
blue.setCircle(blue.getCircle()+1f);//getCircle is initially zero and gets incremented by 1 for everytime the program loops with the user holding the left button.
blue.setXfrontTR((float)Math.cos(Math.toRadians(blue.getCircle())));//Changing top-right x coordinate of the middle square
blue.setZfrontTR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f)))); //Changing top-right z coordinate of the middle square.
blue.setXfrontTL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontTL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing top-left x,z coordinates
blue.setXfrontBL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontBL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing bottom-left x,z coordinates
blue.setXfrontBR((float)Math.cos(Math.toRadians(blue.getCircle())));
blue.setZfrontBR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f))));//Changing bottom-right x-z coordinates
}
If you give each object that requires independent movement a model-view matrix you can achieve this. The other option to quickly draw/move a few independent objects is to:
for each object:
pushMatrix()
draw object
popMatrix()
while in the modelview matrix...
The method of drawing depends greatly on the OpenGL version you're coding to but the above will work for simple drawing. I'm not an expert on OpenGL / 3D programming so, if you wait a bit you may hear(see) better wisdom than what I offer :)
I am trying to scale a shape that I have in Java around a certain point.
When I use the AffineTransform.scale method, it scales based on the top left corner. Is there anyway to scale anchored at a point (say the center of the window for this case).
Thanks,
Ty
I agree with Hovercraft Full of Eels that the proper way to do this is to translate the center to the top left corner, scale, then translate the top left corner back to the center.
However, if you want it to perform it in fewer than three steps, the transform is:
x ⟼ S(x – c) + c = Sx + (c – Sc),
where S is the scaling transformation, and c is the center in coordinates relative to the top left.
So, you need to do your scaling, then translate by c – Sc.
I'm trying to properly configure my Camera and Sprites in libGDX to show up in a 2D coordinate system properly with the origin at the bottom left hand corner.
I set up my Camera like this:
cameraWidth = Gdx.graphics.getWidth();
cameraHeight = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, cameraHeight/cameraWidth);
And I set up my Sprites like this:
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setScale(scale);
sprite.setPosition(startX,startY);
My problem is with sprite.setSize(x,y). If I set all the sprites to have a size of (1, texture aspect ratio), then everything draws with the right display ratio (not smushed or stretched), but nothing draws in the correct place. For example, if I draw something at (0,0), it will draw with its bottom left corner off the left side of the screen and up a number of pixels.
I've noticed by changing around the ratio I can get things to draw in different places - namely if I set it to (1, display aspect ratio) things look pretty close to drawing in the right place - they just draw from their center, not their bottom left corner, as LibGDX specifies. The only problem is that the images all appear as smushed or stretched, which is no good.
This seems like a simple problem and I just want to know how to set this up so I can have a sensible coordinate system that draws things in the right place and in the right aspect ratio. Thanks.
Once you change your viewport to match the screen's aspect ratio then (0, 0) will no longer be at the bottom left of the screen unless the screen is square. If the screen is wider than it is high then the visible portion of the x axis will still go from 0.0 to 1.0, but 0.0 on the y axis will now be somewhere off the bottom of the screen.
If you adjust the camera so that (0, 0) is at the bottom left of the screen, and remember that the visible y axis will only go up to grapicsHeight / graphicsWidth then that should solve your coordinate problem.
I would recommend setting the camera to point to the middle of the screen rather than the bottom left. There's an example here that does exactly that, drawing a 2:1 rectangle which is always in the centre of the screen, always with a 2:1 ratio no matter how much you resize it.
I've found a solution to this problem:
Set the camera to ortho (even though it's already an orthographic camera)
camera.setToOrtho(false,1,screen height / screen width);
Also, each sprite must have its position set to (x - sprite.getWidth()/2, y - sprite.getHeight()/2. I extended the Sprite class and overrode the setPosition method to account for this. Now, every time the position is set, the Sprites end up going where you "would think they'd go", with setPosition(0,0) putting it in the bottom left and setPosition(1,height/width) in the top left.
Oddly enough, this draws every sprite centered around the (x,y) point, which would make sense since width/2 and height/2 were subtracted from the position, except not subtracting the values does not make setPosition center the sprite via the bottom left corner - it's centered in a way I haven't figured out.