I am currently trying my hands on a GameEngine and later on a Game and want to create an abstract class Hitbox, which gets extended by RectHitbox. Now when I create a Polygon Object, which I want to use to keep track of the shape, it also asks me for number of points npoints. I looked at this documentation but I don't know if "total number of points" refers to just the corner points or ALL points inside the area aswell.
Now my question is: is there is any advantage to automatically calculate the parameter npoints or if there is a disadvantage to leaving it Null.
What I mean by automatically calculate npoints (if it refers to the corner points):
public Hitbox(int x1, int y1, int x2, int y2){
xpoints[0] = x1;
xpoints[1] = x2;
ypoints[0] = y1;
ypoints[1] = y2;
polygon = new Polygon(xpoints, ypoints, xpoints.length+ypoints.length);
}
and what I mean by leaving it null:
public Hitbox(int x1, int y1, int x2, int y2){
xpoints[0] = x1;
xpoints[1] = x2;
ypoints[0] = y1;
ypoints[1] = y2;
polygon = new Polygon(xpoints, ypoints, null);
}
Related
I am working on a Java 2D video game, and I am looking for a faster Collision Detection algorithm than what you see below. I am trying to detect a collision between a torpedo and a ship. The algorithm I am using seriously impacts the performance of my main Graphics 2D dispatch loop, slowing down my screen repaints. Was wondering if anyone could recommend a better algorithm for how to handle this, that was quicker to detect a collision? Pointers to sample code would be great!
Here is the slow algorithm that I am using that goes Pixel by Pixel..
private boolean isPixelCollide(double x1, double y1, VolatileImage image1,
double x2, double y2, VolatileImage image2) {
double width1 = x1 + image1.getWidth() -1,
height1 = y1 + image1.getHeight() -1,
width2 = x2 + image2.getWidth() -1,
height2 = y2 + image2.getHeight() -1;
int xstart = (int) Math.max(x1, x2),
ystart = (int) Math.max(y1, y2),
xend = (int) Math.min(width1, width2),
yend = (int) Math.min(height1, height2);
// intersection rect
int toty = Math.abs(yend - ystart);
int totx = Math.abs(xend - xstart);
for (int y=1;y < toty-1;y++){
int ny = Math.abs(ystart - (int) y1) + y;
int ny1 = Math.abs(ystart - (int) y2) + y;
for (int x=1;x < totx-1;x++) {
int nx = Math.abs(xstart - (int) x1) + x;
int nx1 = Math.abs(xstart - (int) x2) + x;
try {
if (((image1.getSnapshot().getRGB(nx,ny) & 0xFF000000) != 0x00) &&
((image2.getSnapshot().getRGB(nx1,ny1) & 0xFF000000) != 0x00)) {
// collide!!
return true;
}
} catch (Exception e) {
// System.out.println("s1 = "+nx+","+ny+" - s2 = "+nx1+","+ny1);
}
}
}
return false;
}
You can define bounds of each object on your scene with Rectangle class and then use Rectangle#intersect(Rectangle rect) method. So your intersect method can look like this:
private boolean intersect(double x1, double y1, VolatileImage image1, double x2, double y2, VolatileImage image2) {
return (new Rectangle(x1, y1, image1.getWidth(), image1.getHeight()).intersect(new Rectangle(x2, y2, image2.getWidth(), image2.getHeight()));
}
That's a lot of getRGB(). Have you considered representing the torpedo and a ship as boxes first and use it for some early reject mechanism?
Also try dividing the pixel tests by 2. Test half the resolution by skipping along every other pixel. Half resolution shouldn't be much of a problem depending on your picture.
I'm trying to print out an array of 10 objects. For some reason though, when I print out the array, there are anywhere from 15 to 22 elements in the array. I can't figure out why. Can someone please point me in the right direction?
import java.util.Random;
public class Main {
public static void main( String[] args ) {
Shape[] myShapes = new Shape[10]; //Array of 10 shapes
Random rand = new Random(); //Random number generator
int shape, x1, y1, x2, y2, x3, y3;
double radius;
for( int i = 0; i < 10; i++ ) {
shape = rand.nextInt(3); //randomly select shape
switch( shape ) {
case 0: //Triangle
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
x2 = rand.nextInt(101);
y2 = rand.nextInt(101);
x3 = rand.nextInt(101);
y3 = rand.nextInt(101);
myShapes[i] = new Triangle( x1, y1, x2, y2, x3, y3 );
System.out.println("Triangle: " + myShapes[i].area());
case 1: //Rectangle
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
x2 = rand.nextInt(101);
y2 = rand.nextInt(101);
myShapes[i] = new Rectangle( x1, y1, x2, y2 );
System.out.println("Rectangle: " + myShapes[i].area());
case 2: //Circle
radius = rand.nextDouble()*100.0;
x1 = rand.nextInt(101);
y1 = rand.nextInt(101);
myShapes[i] = new Circle( radius, x1, y1 );
System.out.println("Circle: " + myShapes[i].area());
}
}
}
Can you please use break; for each case?
Your array actually has 10 elements. But it puts content in each elements up to three times - because control continues from case to case. Thus, if case 0 is right, it will put three shapes and print three prints. If case 1 is right, it will put two shapes and print two prints.
If you put break after each case, then on each iteration it will just put one shape and print just once.
I use QuadCurve2D (Quadratic Curve Segment) in Java. Can I get y-coordinate by x-coordinate from this object?
Something like this:
QuadCurve2D q = new QuadCurve2D.Float ();
q.setCurve (x1, y1, ctrlx, ctrly, x2, y2);
int X = 100;
int Y = q.getY (X); // (?)
I need to cover some polygon with rectangles here's an example :
The black figure in a black square is the polygon that i need to cover with those green rectangles but i need to do it more efficiently that just make a net like i did. Because as you can see there can be place more green rectangles if i moved them.
Rectangles inside are fixed size(just not as big as polygon it self), one for all like in the picture, they can be places vertically and horizontally, i want to fill the polygon as much as it can fit it inside of it, this polygon is just for example, there can be different polygons with holes in them for example that black small square is a hole.
module = rectangle
private void coverWithModules(Graphics g, int[] xpoints, int[] ypoints) {
Polygon module;
int x1, x2, x3, x4, y1, y2, y3, y4;
int moduleRowNumber = 0;
int totalRows = (getMax(ypoints) / moduleHeight);
while (moduleRowNumber < totalRows) {
// first module
x1 = getMin(xpoints);
y1 = getMin(ypoints) + distance * moduleRowNumber + moduleHeight
* moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] x = { x1, x2, x3, x4 };
int[] y = { y1, y2, y3, y4 };
module = new Polygon();
// check if point are inside the polygon
checkModulePlacement(g, x, y, module);
// placing modules in a row
while (x1 < getMax(xpoints)) {
x1 = x2 + distance;
y1 = getMin(ypoints) + distance * moduleRowNumber
+ moduleHeight * moduleRowNumber;
x2 = x1 + moduleWidth;
y2 = y1;
x3 = x1 + moduleWidth;
y3 = y1 + moduleHeight;
x4 = x1;
y4 = y1 + moduleHeight;
int[] xx = { x1, x2, x3, x4 };
int[] yy = { y1, y2, y3, y4 };
module = new Polygon();
checkModulePlacement(g, xx, yy, module);
}
moduleRowNumber++;
}
}
private void checkModulePlacement(Graphics g, int[] x, int[] y, Polygon module) {
boolean pointInside = true;
boolean pointOnObstraction = true;
for (int i = 0; i < x.length; i++) {
if (pointInside) {
pointInside = roof.contains(x[i], y[i]);
}
module.addPoint(x[i], y[i]);
}
pointOnObstraction = checkForObstractions(module);
g.setColor(Color.GREEN);
if (pointInside == true && pointOnObstraction == false ) {
g.drawPolygon(module);
}
}
I was looking for something and i have found Something like this maybe there is more stuff like this ?
I don't know where to search for such info. What should i look up to get what i need ? Maybe there is some kind of library for this kind of things ?
I'm drawing lines in a JFrame on a self made gridPanel.
Problem is, I draw the lines between 2 points. When I have a line that is between point 1 and point 2 and a line between point 2 and point 3, the lines should connect. This however isn,t the case, there is a small gap in between, no idea why. But it isn't drawing till the end of the specified point. (start point is correct.)
Here is the code of the JFrame:
public void initialize(){
this.setLayout(new BorderLayout());
this.setPreferredSize(new Dimension(500, 400));
gridPane = new GridPane();
gridPane.setBackground(Color.WHITE);
gridPane.setSize(this.getPreferredSize());
gridPane.setLocation(0, 0);
this.add(gridPane,BorderLayout.CENTER);
//createSampleLabyrinth();
drawWall(0,5,40,5); //These are the 2 lines that don't connect.
drawWall(40,5,80,5);
this.pack();
}
drawWall calls a method that calls a method in GridPane.
The relevant code in gridPane:
/**
* Draws a wall on this pane. With the starting point being x1, y1 and its end x2,y2.
* #param x1
* #param y1
* #param x2
* #param y2
*/
public void drawWall(int x1, int y1, int x2, int y2) {
Wall wall = new Wall(x1,y1,x2,y2, true);
wall.drawGraphic();
wall.setLocation(x1, y1);
wall.setSize(10000,10000);
this.add(wall, JLayeredPane.DEFAULT_LAYER);
this.repaint();
}
This method creates a wall and puts it in the Jframe.
The relevant code of the wall:
public class Wall extends JPanel {
private int x1;
private int x2;
private int y1;
private int y2;
private boolean black;
/**
* x1,y1 is the start point of the wall (line) end is x2,y2
*
* #param x1
* #param y1
* #param x2
* #param y2
*/
public Wall(int x1, int y1, int x2, int y2, boolean black) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.black = black;
setOpaque(false);
}
private static final long serialVersionUID = 1L;
public void drawGraphic() {
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(black){
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(8));
} else {
g2.setColor(Color.YELLOW);
g2.setStroke(new BasicStroke(3));
}
g2.drawLine(x1, y1, x2, y2);
}
}
So, where am I going wrong? The true/false is to determine if the wall should be black or yellow, nothing to be concerned about.
You've set the main layout as BorderLayout using this.setLayout(new BorderLayout());
You then add the GridPane to the center position this.add(gridPane,BorderLayout.CENTER);
You then try and add the walls to the main layout using this.add(wall, JLayeredPane.DEFAULT_LAYER); ... But the main layout is a BorderLayout
This is going to cause you some issues
UPDATED
The other problem you have is in the Wall#paintComponent method.
You are drawing the lines offset from the x1 and y1 positions, but the component has already being positioned at this point.
The top, left corner of any component is always 0x0
The line g2.drawLine(x1, y1, x2, y2); should read more like...
int x = x2 - x1;
int y = y2 - y1;
g2.drawLine(0, 0, x, y);
UPDATED
You should also avoid setting the size of your components to some arbitary value (such 1000x1000) and rely more on the ability for your components to give you feedback...
public Dimension getPreferredSize() {
int width = Math.max(x1, x2) - Math.min(x1, x2);
int height = Math.max(y1, y2) - Math.min(y1, y2);
if (black) {
width += 8;
height += 8;
} else {
width += 3;
height += 3;
}
return new Dimension(width, height);
}
Then when adding the Wall, you can use wall.setSize(wall.getPreferredSize()) instead...