Need Help Moving An Object - java

I need ideas about how to move an object (a circle which represents a robot in my application).
The surface the object will be moving on consists of Tiles of BufferedImage of 3 rows and 3 columns ( represented in arrays). All the Tiles are equal in sizes (160 X 160).The 3rd row and the 3rd column are the track rails on which the object must be moving on.
It means that the object(robot) can move horizontal (forward and backward) and vertical (upwards and downwards).
The Tile at the position [2][2] (please am counting from top. so the top row will be 0 the next afterward is 1 etc.. ) is a crossing which the robot will use to change to the vertical track rails or to the horizontal track rails.
My problem now is how to move the object to a specific Tile after the crossing has turned. For instance the robot will be in the Tile at position [2][1] and want to move to the tile at position [1][2] after the crossing is turned and then move further upwards. Or it can be at [1][2] and want to move to [2][1] after the crossing is turned and then move further backwards .
How can i move the robot from one Tile to another Tile? Which way can i refer to a specific Tile in the BufferedImage that i can place the object. All what i want is give me the ideas how i can do it.
Please this is my first time of doing such project so forgive me if my question is too elementary. With your explanation and help i will learn more from it.
Thank you very much.

In order to display your image you need to figure out the bounds of the grid you want to put your image into. I usually create two helper methods, one to translate grid coordinates into display coordinates and the other one to go the other direction.
private Point convertGridToDisplay(int x, int y) {
return new Point(x * 160, y * 160);
}
private Point convertDisplayToGrid(int x, int y) {
return new Point(x / 160, y / 160);
}
convertGridToDisplay() will give you the upper left hand coordinate where you should draw your image to.
For example:
Point point = convertGridToDisplay(2, 1);
graphics.drawImage(img, null, point.x, point.y)
will draw your image at grid (2, 1).
convertDisplayToGrid() will come in handy when you want to figure out which grid a mouse click was made in.

Related

I want to inscribe rectangle into a grid. How to get the list of grid cells that collide with the rectangle

I'm coding a collision system for my 2D game engine in Java but I'm having problems with obtaining certain values. Lets say I have a rectangle and want to inscribe it into a grid. I want to list every grid cell which collide with the rectangle. As of rectangle what I know is its width, height, center point (x, y), angle in radians. As of cell, the coordinates of each cell are basically (n * size, m * size) where n, m = -2, -1, 0, 1, 2... (like in the image). I've been trying to find a fast solution for a long time but with no luck. I have also created a reference image for you to better understand my problem. The pink cells are the ones I want. I hope there's someone who had a similar problem and is willing to help me out :) Best of luck in your projects.

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.

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

Dragging an object in Swing

I have a problem which I have to solve with my code. So I have a checkerboard and when I click on something I want to drag around (i.e. checker), I make visible a label which holds picture of it (I know it ain't the neat solution but I was told to do it so) in a proper place calculated by a fomula. I wanted to use the same formula to be able calculate coordinates of the checker under mouseDragged event but here's the problem. If I just set the location of the label by simple getX() or getY() from the event then it's top left corner is "jumping" under my mouse pointer as soon as I move the mouse. I'd like it to stick to the cursor in the place I clicked it, so I did something like this:
public void mouseDragged(MouseEvent evt) {
int col = (evt.getX() - 4) / 40;
int row = (evt.getY() - 4) / 40;
int setX = evt.getX()-(col*40);
int setY = evt.getY()-(row*40);
if (dragged)
inv.setLocation(evt.getX()-setX+42, evt.getY()-setY+37);
where col is the X coordinate of the column I want to drage the checker from (and so is the row) and setX is just a variable I'm using below. And these calculations seem to be ok to me, but they make me able only to drage the checker in a manner where it doesn't move at all (like I was dragging something transparent) and when I enter finally a new place on the board it's being painted there and so on. Not a fluent drag but it's being consequently drawn in the fields under my cursor during dragging.
The +42 and + 37 are just some numbers to calcualte it in proper place since the board isn't starting from (0,0).
The reason the corner of the checker is "jumping" to your mouse is because of the way images are drawn. When you set the coordinates of the image you are setting the point P(0,0) of the image, which is the top-left corner when drawn on a screen.
To fix this issue, in addition to keeping track of the row and column number, you need to keep track of the x and y offset (in pixels) you want to maintain though the mouse movement in each checker board "cell".
This means when you click, you might click the checker in row 2, column 3, with an (x, y) offset of (12,14). I am assuming your squares are larger than 14x14 pixels.
I turned your 42 into BOARD_X_OFFSET and 37 into BOARD_Y_OFFSET
I have added two doubles cellX and cellY which hold the offset within the cell.
This will make your set location look similar to this:
inv.setLocation(evt.getX()-setX+BOARD_X_OFFSET+cellX, evt.getY()-setY+BOARD_Y_OFFSET+cellY);

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