How to know collision between 2 images javafx -
how collision of 2 images or bounds intersect on javafx? move 1 image if touch second image want trigger something.
gc.drawimage( img2, 0, 400, 950, 100 ); gc.drawimage( img3, 200, 200, 150, 50 ); if (event.getcode() == keycode.right ){ if(img2.intersects(img3.getboundsinlocal())){ dosomething(); } }
this code doesn't work
you can create wrapper image, sprite class example, , can use wrapper give x,y coordinates. can intersection of rectangle2d shape.
import javafx.geometry.rectangle2d; import javafx.scene.canvas.graphicscontext; import javafx.scene.image.image; public class sprite { private image image; private double positionx; private double positiony; private double width; private double height; public sprite(image image) { this.image = image; width = image.getwidth(); height = image.getheight(); positionx = 0; positiony = 0; } public void setposition(double x, double y) { positionx = x; positiony = y; } public void render(graphicscontext gc) { gc.drawimage(image, positionx, positiony); } public rectangle2d getboundary() { return new rectangle2d(positionx, positiony, width, height); } public boolean intersects(sprite spr) { return spr.getboundary().intersects(this.getboundary()); } }
Comments
Post a Comment