Vote count:
0
So I am making this top-down game where there are zombies and your player can shoot them. From the player there is a red line (like a laser) that shows which direction you are pointing. I wanted this laser to act like a real laser - if there is a object in the way it will only go as far as that object. Also I am not really sure how to make the laser go through the mouse pointer and keep going to the edge of the window (unless object in the way). I have an ArrayList called entities which contains all of my objects (Class = Entity) that I render.
Player p = (Player) entities.get(0);
g.setColor(Color.RED);
g.drawLine((int)p.getX(), (int)p.getY(), getMouseX(), getMouseY());
That code just makes a line that goes to the point of the Cursor (as expected) from the Players location. Bear in mind that the enemies are circles (I have a method to check if a point is in a circle)
public boolean isPointInCircle(double centerX, double centerY, double radius, double x, double y){
boolean isInRectangle = x >= centerX - radius && x <= centerX + radius && y >= centerY - radius && y <= centerY + radius;
if(isInRectangle){
double dx = centerX - x;
double dy = centerY - y;
dx *= dx;
dy *= dy;
double distanceSquared = dx + dy;
double radiusSquared = radius * radius;
return distanceSquared <= radiusSquared;
}
return false;
}
I tried to go about this by sending an invisible "bullet" at the gradient of the line but it failed so I gave up on that. So my two questions are:
How do I make the line go in the direction of the mouse but keep going to the edge of the frame?
... Unless it intersects with an Entity, at which point it will only go as far as the Entity?
Make line "stop" when it intersects object
Aucun commentaire:
Enregistrer un commentaire