Libgdx Y axis, how to invert it? - java

How to invert Y axis? When I touch on bottom or top of the screen, the Y value is opposite I want

You can't invert an axis per se. You see, in computer graphics, the 2D coordinate system is a bit different from the canonical one taught at school in maths. The difference is that in computer graphics the y-axis is in the opposite direction, that is, from the origin to the bottom it has positive values and from the origin to the top it has negative values. Also, the origin is at the top left corner of the screen. If you can't get used to it then you can always take the opposite value to what you get, for this, asume ycoord holds the value obtained then you can do ycoord = -ycoord and that will get you the value as you're used to. Also, if you want the origin to be in the bottom left corner then you should check your y-coordinate, if it's positive then substract the vertical resolution to it, and if it's negative then add the vertical resolution to it.
But keep mind that you're going against the standard definition for coordinate systems in computer graphics.

I would say this is a duplicate questions of this one:
Move a shape to place where my finger is touched
Check on my answer there, so I won't repeat my self.
Or in short - use camera.unproject() method to get world coordinates from screen coordinates.

Related

Why is my pointer y coordinate reversed?

When I click on the top left corner it gives me 0,0 but I want the opposite result. I want to have a 0,0 coordinate when I click on the bottom left corner.
Basically when I go down the y coordinate is higher and when I go up the y coordinate is lower.It only happens when I do Gdx.input.getY();
This is true for almost any computer language or function.
The reason for this goes back. In earlier computers Cathode Ray Tubes (CRTs) would "draw" the image with a cathode ray from the upper left corner to the lower right.
To ease the interface between the graphics card memory and the CRT, the memory was read from the beginning and the image was drawn from the top left (with the lowest memory address) to the lower right (with the highest memory address).
The reason for this order likely originates from the writing style in countries where the computer was invented.
The tradition of computing from left to right and top to bottom persists today, thus the origin for the plane field in in the upper left corner.
Use the Camera's unproject method, which takes a Vector3 holding screen coordinates and converts them to camera (or world) coordinates. Try something like:
camera.unproject(screenCoords.set(screenX, screenY, 0);
Note, this will modify screenCoords to hold world coordinates (in your case... flipping the y, and taking into account any translation of the camera).

How to do 2D ground with depth sense in libgdx?

I know do a horizontal and vertical scroller game (like Mario), in this game type, the character is always in the same distance from user. The character only moves to left and right in horizontal scroller and to down and up in vertical scroller.
But there are games in 2D that the character can move freely in the scene, like for example graphic adventures.
So how can I do that ground in order to move the character freely on the ground with depth sense?
An example can see in this video: http://youtu.be/DbZZVbF8fZY?t=4m17s
Thanks you.
This is how I would do that:
First imagine that you are looking at your scene from the top to the ground. Set your coordinate system like that. So all object on your scene will have X and Y coordinates. All your object movements and checking (when character bumps into a wall or something), calculations do in that 2D world.
Now, to draw your world, if you want simpler thing, without some isometric perspective 3D look you just to draw your background image first, and then order all your objects far to near and draw them that way. Devide your Y coords to squeeze movement area a bit. Add some constant to Y to move that area down. If you characters can jump or fly (move trough Y axe) just move Y coord to for some amount.
But if you want it to be more 3D you'll have to make some kind of perspective transformation - multiply your X coordinate with Y and some constant (start with value 1 for constant and tune it up until optimal value). You can do similar thing with Y coord too, but don't think it's needed for adventure games like this.
This is probably hard to understand without the image, but it's actually very simple transformation.

Rotating a Sprite in Java

While working on Projectiles I thought that it would be a good idea to rotate the sprite as well, to make it look nicer.
I am currently using a 1-Dimensional Array, and the sprite's width and height can and will vary, so it makes it a bit more difficult for me to figure out on how to do this correctly.
I will be honest and straight out say it: I have absolutely no idea on how to do this. There have been a few searches that I have done to try to find some stuff, and there were some things out there, but the best I found was this:
DreamInCode ~ Rotating a 1-dimensional Array of Pixels
This method works fine, but only for square Sprites. I would also like to apply this for non-square (rectangular) Sprites. How could I set it up so that rectangular sprites can be rotated?
Currently, I'm attempting to make a laser, and it would look much better if it didn't only go along a vertical or horizontal axis.
You need to recalculate the coordinate points of your image (take a look here). You've to do a matrix product of every point of your sprite (x, y) for the rotation matrix, to get the new point in the space x' and y'.
You can assume that the bottom left (or the bottom up, depends on your system coordinate orientation) of your sprite is at (x,y) = (0,0)
And you should recalculate the color too (because if you have a pure red pixel surrounded by blue pixel at (x,y)=(10,5) when you rotate it can move for example to (x, y)=(8.33, 7.1) that it's not a real pixel position because pixel haven't float coordinate. So the pixel at real position (x, y)=(8, 7) will be not anymore pure red, but a red with a small percentage of blue)... but one thing for time.
It's easier than you think: you only have to copy the original rectangular sprites centered into bigger square ones with transparent background. .png files have that option and I think you may use them.

Setting the pivot point of a JPanel to its center

I am building a Java application that is going to feature two circles of random sizes that need to be clicked by the user. The time between the click on the first and the second circle is going to be measured. Unfortunately, since I am new to Java so things have been slow for me. Currently I have my application draw circles and measure time between clicks using System.nanoTime() but now I am running into a problem.
Because the circles need to be a fixed distance away from eachother I want to use the center of the circles as the origin points. So basically I want to be able to provide coordinates for the circle so that the center of the circle should be at those coordinates. The distance between the circles then describes the distance between the centers. My circle currently is embedded into a JPanel but if I set the JPanel's position it moves the top left to that position.
Of course I have done some searching read that I may need to play around with either AffineTransform or Graphics2D.translate() which I have tried in paintComponent() but this got a bit confusing so then I tried to override setlocation and subtract the radius from the position. It sort of works but it is not the most clean solution. Can aonyone give me some pointers on how to do this?
Thanks in advance.
If I understand the problem statement, all such pairs of circles will lie on opposite sides of a circle centered in the enclosing panel, as shown here. Simply choose a random 0 ≤ θ < π and find its opposite at π - θ. Note how the example's rendering scales as the panel is resized.
As an aside, the example uses setPreferredSize() to establish the dimensions of the drawing panel, but you may want to override getPreferredSize() instead.
Addendum: The example uses fillOval() to render the circles, but you can use draw() with any desired Shape; the latter provides several contains() methods suitable for hit testing, as mentioned here.
You have the coordinates for the two center for the circle (x1, y1) and (x2, y2).
The size of the radius is random.
Once you have the radius of the two, r1 and r2, simply position them at (x1-r1, y1-r1) and (x2-r2, y2-r2).
You can use java.awt.Point to represent the center, and use
center.translate(-radius, -radius)
and use the new translated value as position for the drawing.
Maybe you think it is not a clean solution, but why not? Everything in Java is painted by giving the top left corner for the position, so is the use of the center that is not clean :).
To calculate the left top position by doing -radius is clean :)

Ray tracing - constructing ray through pixel

I have an assignment to implement ray tracing in Java.
I'm not asking for much, just to have some information on how to construct the rays from the camera through a pixel given its x and y. I've found over the Internet a lot of sources that explain that but in 2D, and I need how to do that in 3D.
Thanks in advance
The question is how to find the coordinates in space of a point on the screen whose position is given by (x,y) in screen coordinates.
I don't know what coordinates system you're using for the screen, so I'll make some educated guesses and you can adjust accordingly.
The center of the screen has known location [X,Y,Z]center in space. I'll presume the origin of the screen coordinate system is there. We have a "direction" vector d which is normal (perpendicular) to the screen, and an "up" vector u. I'll presume that the +y direction on the screen is u. We can take the cross-product of these vectors, r = dxu, which I will take to be the +x direction on the screen. So the location of a point on the screen whose screen coordinates are (x, y) will be [X, Y, Z]center + xr + yu, and we're done.
The basic idea is this: you have a camera at a given position (x,y,z) with a given resolution. You have a set of light sources. You have an orientation of the camera (an angle, think of how you'd tilt/rotate your head to look up/down etc...). Now what you want to do is essentially for each camera pixel, "extend" perpendiculars (rays) until they touch geometry. Then you know "what to render" (namely the bit of geometry you've just found). Next up is to determine whether or not the object is shadowed, which you do by "extending" rays towards the light sources until they either touch geometry (your spot is shadowed by the other bit of geometry, for the given the light source) or until they reach the light source (your spot is lit by the given light source).
That's the basics, it gets a lot more difficult when you consider things like reflection, diffusion of light, and so on.

Categories