Relation between x and y axes and pixels in Java - java

I am making the game Breakout in Java and I have one slight problem:
if(ballLocation.y >= 430){ //baseline
if((ballLocation.x >= batLocation.x) && (ballLocation.x <= (batLocation.x + width))){ //determining whether or not the ball is touching the bat by establishing a range of values
directionY = -2; //changing the direction of the ball so that it appears to be bouncing off when it hits the bat
}
}
If I hit the ball on the bat further to the left the if statement is true, however for some reason if it hits further to the right it is false even though I can see that the ball is hitting the bat. So I think that the only problem can be with adding the width variable to the batLocation.x, because batLocation.x is in terms of units on the x axis, and the width is the width of the image (as the bat and ball are both png files and not just drawn by the game) in terms of pixels (which is 10).
So I am thinking that the most likely reason why my code is not working is because I am making a false assumption and assuming that if I add the width in pixels to the starting x point that I will get the length of the bat when perhaps 1 pixel is not equal to 1 unit on the x axis.
So is this the case? Is 1 pixel not equal to one unit in terms of the x or y axes? And if it is not, then how many units in terms of x is my bat's width if it is 10 pixels in width?
I have got the latest version of JDK 8.

There isn't really a built-in x and y axis, except when you draw, and that's always in pixels. What units have you made x and y in? When you draw, do you use those values as the screen location? If so, try RealSkeptic's suggestion; otherwise, the units are however you defined them.

Related

How to use Math.atan2 to move an object to a specific place

Imagine I put an object on the screen at the point
X=6; e Y =60;
Now I'm trying to do that:
double radius = Math.atan2(Object.x(), Object.y());
this.dx2 =Math.cos(radius);
this.dy2 =Math.sin(radius);
From the right side of the screen it will appear random circles.
The hight of the screen is 160, so from 0 to 160 it can appear a circle.
The X of those circles is always the same (the maximum width).
They come from right to left (where the target is).
I'd like all of them to go to this target.
The idea is do that for moving the circles on update method:
x -=(dx2 * spd); y -=(dy2 * spd);

LWJGL Can't get coordinates to match up

Alright so i'm more than certain that this can be solved with some basic math however I for the life of me cannot find the answer. I have two sets of x and y coordinates which are almost 100% alike. For example when I change the x of the first coordinate it also changes the x of the second coordinate. The issue is that the second coordinate seems to move at a faster rate than the first one does. Note that the first coordinate that I am talking about is x which is based on the players movement and the second is mouseX which is where the mouse should be at in the game, not on the screen. Here is some code that might help.
So in short my mouse moves perfectly in sync with the player however I need to convert the mouse coordinates to match the player coordinates.
Mouse Manager
Note that calculate is where I am trying to calculate the game coordinates in relation to X.
Game Manager
As an example here are some coordinates from me moving just to the right. Also I did not move the mouse other than how it moves from the player position.
FORMAT: {Player x, player y} {Mouse x, mouse y}
{24 19} {219 212}
{8 19} {475 212}
{-14 19} {827 212}
{-36 19} {1179 212}
Alright so I looked at it a bit harder and noticed a correlation between them and from that I was able to construct the equation -(((MouseX-217)/16)-24) = new X.
The -217 is to zero out the actual coordinates, the 16 for the pixel to position size and the 24 for the lock position size.

Drawing a square by dragging the mouse

I am writing a Java program that closely mimics Microsoft Paint. It can draw four different shapes: Lines, Ovals, Rectangles, and Squares. I am very close to finishing this but I am stuck on the logic for drawing squares.
There are two Points involved while drawing these shapes. The first Point(point1) is when the user presses the mouse button and the second Point(point2) is while the user drags the mouse across the canvas. I believe drawing Rectangles and Squares should be quite similar but the part that has confused me is when drawing a square the sides are equal length so point2 isn't exactly where the mouse is.
Here is the fillRect() method header for reference:
fillRect(x, y, width, height)
My functioning code for drawing rectangles is as follows:
g.fillRect((((point1.x < point2.x) ? point1.x : point2.x)),
((point1.y < point2.y) ? point1.y : point2.y),
Math.abs(point2.x - point1.x),
Math.abs(point2.y - point1.y));
I tried using the same code for drawing squares except changing the height parameter to be equal to the width parameter because squares have equal length sides:
g.fillRect((((point1.x < point2.x) ? point1.x : point2.x)),
((point1.y < point2.y) ? point1.y : point2.y),
Math.abs(point2.x - point1.x),
Math.abs(point2.x - point1.x)); //same as width
I don't know what the problem is with making both width and height equal. It works when drawing downwards to the left or right but of course the shape does not expand if you pull straight down. Clicking and dragging up does not work; the square simply moves up with the mouse along the Y axis instead of expanding.
Could anyone point me in the right direction regarding the logic for drawing square from two points?
I can explain my code a bit better if need be.
If I were using a tool to draw a square, I would expect that my mouse cursor would remain on one of the sides of the square while drawing. If you always use the x distance as the side, then if I've drawn farther down than across, the cursor will be outside the square.
I think, while the dragging is going on, the code would need to calculate whether the x distance or the y distance to the origin is the longer, and use that as the side of the square-in-progress. Then the cursor will be on one of the sides, and that side will extend out beyond the cursor to the corner of the square-in-progress.
I don't know why the drawing is going awry -- it is difficult to be sure I understand what you say is going wrong.
You should do two calculations in your MouseDragged method -- first you should calculate the two corner Points of the rectangle, p1, and p2, and only then should you calculate the width. Point p1 is easy -- it's always the first Point pressed, but p2 will require a simple calculation. Once these are clarified, your calculations should fall out correctly.
i.e., something like:
int width = Math.abs(pointA.x - pointB.x);
int height = Math.abs(pointA.y - pointB.y);
width = Math.max(width, height);
height = width;
int x = pointA.x > pointB.x ? pointA.x - width : pointA.x;
int y = pointA.y > pointB.y ? pointA.y - width : pointA.y;
g.fillRect(x, y, width, height);

Scale dimensions in a physics simulation

I need some help here with a physics simulation in java I'm writing. The simulation is about the free fall of a body. I'm using java and I don't use any third-party library.
I have an applet (1400px wide, 700px high) and a sprite (which is oval) falling down. The gravity is set to 10 m/s². I apply second Newton's Law to my oval sprite, and I use RK4 algorithm to compute the x an y coordinates of my sprite over time.
This all works fine...Except that I don't know how to scale the dimensions I use in my simulation.
For example, I would like 1px to represent 1cm (both width and height). So that my 1400px*700px applet dimensions will represent 14m*7m in real. I used
Graphics2D.scale()
method but it does't seem to work. I also thought to change the gravity but this seems not appropriate for me...
Could someone tell me a proper way to scale my dimensions?
You have a 1400 x 700 pixel applet drawing area.
You have a 14 x 7 meter physics area.
In order to scale from meters to pixels, you have to use a scaling factor.
1400 pixels / 14 meters = 100 pixels per meter.
700 pixels / 7 meters = 100 pixels per meter.
So far so good. If you had two different scaling factors, your drawing area would be distorted.
Let's assume that the oval started at (0, 0).
So we calculate the first position of the oval. Let's assume the first calculated position is
x = 2.45
y = 3.83
So, using the scaling factor we came up with:
pixel x = 2.45 meters x 100 pixels per meter = 245 pixels.
pixel y = 3.83 meters x 100 pixels per meter = 383 pixels.
Our physics area has increasing x to the right and increasing y down. Fortunately, our drawing area has increasing x to the right and increasing y down.
So, we don't have to worry about changing signs.
Draw the oval at (245, 383).
Calculate next x, y position and repeat.

What is Z in a 3D universe?

I'm creating a 3D renderer in Java, which currently can render the wireframe of a cube using Points and lines and rotate the cube, the question is, what should Z be? And what should be set to Z? I'm guessing that the size of the cube should be set to Z?
Thanks for your time! Any answers would be much appreciated.
Z usually means the out-of-plane direction if the current viewport lies in the x-y plane.
Your 3D world has its own coordinate system. You'll transform from 3D world coordinates to viewport coordinates when you render.
I think you might be missing some basic school math/geometry here. However, it's actually not that hard to understand.
Imagine a flat plane, e.g. a sheet of paper.
The first coordinate axis will go straight from left to right and we'll call it X. So X = 0 means your point is on the left border. X = 10 might mean your point is on the right border (really depends on how big you define a unit of 1; this could be in centimeters, inches, etc.). This is already enough to describe some point in one dimension (from left to right).
Now, we need a second axis. Let's call it Y. It's running from the top border (Y = 0) to the bottom (Y = 10). Now you're able to describe any point on the plane as you've got two positions. For example, (0, 0) would be the top left corner. (10, 10) would be the bottom right corner. (5, 0) would be the center point of the top border, etc.
What happens if we add yet another dimension? Call it Z. This will essentially be the height of your point above the sheet. For example, Z = 0 could mean your point is sitting on the sheet of painter, while Z = 10 means your point is sitting 10 cm above the paper. Now you use three coordinates to describe a point: (5, 5, 0) is the center of the paper. (5,5,5) is the center of the cube sitting on your paper filling it and being 10 cm high.
When programming in 3D, you can use the same terminology. The only real difference is, that you're using a so called projection/view matrix to determine how to display this 3d positions on screen. The easiest transform could be the following matrix:
1 0 0
0 1 0
Multiplying this with your 3d coordinates you'll get the following two terms:
2d-x = 3d-x
2d-y = 3d-y
This results in you viewing the cube (or whatever you're trying to display) from straight above essentially ignoring the Z axis again (you can't render something sticking out of your display, unless using some kind of 3d glasses or similar technology).
Overall, it's up to you how you use the coordinates and interpret them. Usually x and y refer to the plane (position on the ground or position inside a 2D world) while z might be the height or the depth (front or back). It really depends on the specific case. But in generic, it's really just another dimension like x and y.
3D means 3 "Dimensions". One dimension is "X", the other "Y", the third "Z". None have a sepcific direction, though it's convenient to conventionally assign a direction, for example "Forward", "Left", and "Up".
Something whose X, Y, and Z values are all equal to 0 resides at the origin, or center of the space. You can write this as (0,0,0) where the order of the parameters are (x,y,z).
A point or vertex at the location (1,0,0) is one unit in the X direction from the origin. So if you moved from (0,0,0) to (1,0,0), you would be moving purely in the X direction.
(0,1,0) is one unit in the Y direction away from the origin.
(0,0,1) is one unit in the Z direction away from the origin.
(1,1,0) is one unit in the X direction and one unit in the Y direction. So if X means "Forward", and Y means "Left", then (1,1,0) is forward-and-left of the origin.
So a basic cube can be defined by the following vertices:
(1,1,-1)
(1,-1,-1)
(-1,1,-1)
(-1,-1,-1)
(1,1,1)
(1,-1,1)
(-1,1,1)
(-1,-1,1)

Categories