Changing image node to another corner - java

So basically i decided to start a small project but i noticed the node of the image is at the upper left corner. So for example, the x and y values are from that corner. I'd like them to be starting at the bottom left corner. How can i change that?
Sorry if node isn't the right name.

Java Component uses the top left as the origin of the axis. So if you it to be at the bottom left, you need to add the height of the component to the y coordonate.
You should set the location to x, y+ component.getHeight()

Related

Libgdx Y axis, how to invert it?

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.

How to get the vertices of a rect() in Processing?

Using the OpenCV for Processing library by Greg Borenstein, I am able to detect a user's face within a webcam stream, and draw a rectangle around it. Here is the portion that draws the rectangle to the screen:
Rectangle[] faces = opencv.detect()
for (int i = 0; i < faces.length; i++)
{
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
As the user moves their face, the rectangle will move accordingly.
I want to draw a line on the screen where one point is set at predefined location and the other is always pinned to the bottom right hand vertex of the rectangle. This way the length and angle of the line will change with respect to the rectangle's location.
One thing I tried was to subtract some values from faces[i].x and faces[i].y until I reached the bottom right vertex, but I found that the depth of the face in the webcam made this method not work.
With that, how could I find the above mentioned vertex of the rectangle so that I can properly draw the line?
The x position of a rectangle gives you its left side. So x+width gives you its right side.
Similarly, the y position of a rectangle gives you its top side. So y+height gives you its bottom side.
float rectRight = faces[i].x + faces[i].width;
float rectBottom = faces[i].y + faces[i].height;
If that's still not what you're looking for, then please post a small example that we can actually run. Use hardcoded values instead of relying on opencv, so we can copy and paste your code and see the problem ourselves.

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).

OpenGL not rendering as expected

If I have the screen setup with the following
gl.glViewport(0,0,width,height); //Reset the current viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //select the projection matrix
gl.glLoadIdentity(); //Reset the Projection Matrix
gl.glOrthof(0f,1f,0f,1f,-1f,1f);
and the vertices set up as follows
this._vertices=new float[]{
0.0f,0.5f,0.0f, //V1 bottom left
0.0f,1.0f,0.0f, //V2 top left
0.5f,0.5f,0.0f, //V3 Bottom right
0.5f,1.0f,0.0f //V4 top right
};
then when drawing I do
gl.glTranslatef(0.0f,0.0f,0.0f);
it places the square in the top left (i thought 0,0 was bottom left?)
and then
gl.glTranslatef(0.5f,-0.5f,0.0f);
places the square in the middle of the screen (suggesting the bottom is actually -1.0f rather than 0.0f)
How do I make the bottom left of my screen start from 0,0?
Update------
I have found that if I change the line
gl.glOrthof(0f,1f,0f,1f,-1f,1f);
to
gl.glOrthof(0f,1f,1f,0f,-1f,1f);
then nothing changes (ie the top left is still 0,0 and bottom left is 0,-1)
however if I leave the line out completely then the origin is in the centre of the screen (ie top left is -1,-1)
The glOrthof is used to create your screen coordinate system, since its input parameters are left, right, top and bottom I can not understand where the confusion lies. If you wish your top left corner is the (0,0) point then just set the top and left parameters to zero and bottom and right to some positive values so that positive X is to the right and positive Y is downwards.
Some very common cases of usage of glOrthof are:
View coordinate system where you insert (0,0) as top-left and (screenHeight , screenWidth) as bottom-right. In quite a few cases the bottom-left is at (0,0) though.
Graph like coordinate system where screen centre is at (0,0) and then top-left is at (1,-1) and bottom-right at (-1,1)
Normalized view system which has the screen centre at (0,0) but height is relative to width depending on the screen ratio. So top-left is at (-1,-(screenHeight/screenWidth)) and bottom-right at (1, screenHeight/screenWidth).
You should note that only in 1st and 3rd cases your square will always be drawn as a square and not as a rectangle (where a/b = screenWidth/screenHeight). So choose which suits your application best.
You should know that glOrthof is also very useful for panning and zooming. Instead of doing some strange translations and scales you can simply change glOrthof parameters so that you display a certain part of your scene.

Finding out which side of a 2D AABBxAABB collided?

I'm making a breakout/brick-breaker/arkanoid clone (opengl-es/android) and I've been stuck on my collision detection code for quite some time. As the title suggests: How do I figure out which side of a brick has been hit by the ball ?
Since I only need to invert the speed in a certain direction, x or y, when a brick is hit I could think of:
if(speedY < 0) : left, upper or right
else : left, bottom or right
if(speedX < 0) : bottom, right or upper
else : bottom, left or upper
however this doesn't bring me far in deciding if it collided vertical or horizontal, and with that, which direction I should send the ball next.
I've looked at some code examples on the internet, however those often are very vague, complicated or off-topic for me.
Well, if you know the position of the Brick and the position of the ball you can do tests on each object to determine the side of the brick.
Assuming the standard Java origin in the top-left:
+----+
( )| |
+----+
If the ball's Max-X is < the Min-X of the brick, you know that it has to be on the left side, and vice versa with the right and left. You would also test the Y values for top and bottom collisions.
Of course this assumes you've gotten the collision detection working first.
EDIT
This is an excerpt from my Collision engine, this is just a small bit for an example, but this is how I test if the object is to the left of the thing it's colliding with.
else if ((oCenter.getX() < sCenter.getX())
&& ((oCenter.getY() < (sCenter.getY() + sourceHalfHeight))
&& (oCenter.getY() > (sCenter.getY() - sourceHalfHeight))))
return LEFT;
In my example here oCenter is a Point2D and it's the center of the ball. sCenter is a Point2D and it's the center of a rectangular shape. sourceHalfHeight is half the height of the rectangular shape (the object with the center point sCenter).
The Pseudo-code algorithm:
if (the center X of the ball < the center X of the rectangle
AND the center Y of the ball is BETWEEN the max Y and min Y of the rectangle)
then the ball is to the LEFT of the rectangle
end if
The thing is, you cannot know it with the speed only, as the ball could hit 2 different sides at 2 different times, both times with the same speed in the same direction.
Ex:
First line hits the top, second one hits the side, but both have the same speed and direction
/
/ /
_____ /
|_____|
You would have to use the ball's position and compare it with each sides of the brick

Categories