Permalink
Browse files

Added a hitBounds member variable (Jfx Circle) for collision detectio…

…n. The initHitZone() method creates the collision bounded region of the ship. Also, I've scaled or translate the missile closer to the nose of the ship which is beyond the hit zone. If both regions are in the center you are shooting yourself. So beware when increasing the size of the hit zone. To make things better update the collide() method. Lastly, I've updated the shieldUp() method now called shieldToggle(). This method will swap the hitBounds or the shield.
  • Loading branch information...
1 parent d462d27 commit 723eae937766f9a5a23622e585c01cc3b25e65ed @carldea committed Jun 11, 2012
Showing with 35 additions and 7 deletions.
  1. +35 −7 demos/navigateship/src/main/java/carlfx/demos/navigateship/Ship.java
@@ -134,14 +134,18 @@
*/
FadeTransition shieldFade;
+ /**
+ * The collision bounding region for the ship
+ */
+ private Circle hitBounds;
+
public Ship() {
// Load one image.
Image shipImage = new Image(getClass().getClassLoader().getResource("ship.png").toExternalForm(), true);
stopArea.setRadius(40);
stopArea.setStroke(Color.ORANGE);
RotatedShipImage prev = null;
-
// create all the number of directions based on a unit angle. 360 divided by NUM_DIRECTIONS
for (int i = 0; i < NUM_DIRECTIONS; i++) {
RotatedShipImage imageView = new RotatedShipImage();
@@ -173,7 +177,31 @@ public Ship() {
flipBook.setCacheHint(CacheHint.SPEED);
flipBook.setManaged(false);
flipBook.setAutoSizeChildren(false);
+ initHitZone();
+
+ }
+ /**
+ * Initialize the collision region for the space ship.
+ * It's just a inscribed circle.
+ */
+ public void initHitZone() {
+ // build hit zone
+ if (hitBounds == null) {
+ RotatedShipImage firstShip = directionalShips.get(0);
+ double hZoneCenterX = 55;
+ double hZoneCenterY = 34;
+ hitBounds = CircleBuilder.create()
+ .centerX(hZoneCenterX)
+ .centerY(hZoneCenterY)
+ .stroke(Color.PINK)
+ .fill(Color.RED)
+ .radius(15)
+ .opacity(0)
+ .build();
+ flipBook.getChildren().add(hitBounds);
+ collisionBounds = hitBounds;
+ }
}
@@ -403,17 +431,17 @@ public Missile fire() {
double offsetX = (shipImage.getBoundsInLocal().getWidth() - m1.node.getBoundsInLocal().getWidth()) / 2;
double offsetY = (shipImage.getBoundsInLocal().getHeight() - m1.node.getBoundsInLocal().getHeight()) / 2;
- // initial launch of the missile
- m1.node.setTranslateX(node.getTranslateX() + offsetX + m1.vX);
- m1.node.setTranslateY(node.getTranslateY() + offsetY + m1.vY);
+ // initial launch of the missile (multiply vector by 4 makes it appear at the nose of the ship)
+ m1.node.setTranslateX(node.getTranslateX() + (offsetX + (m1.vX * 4)));
+ m1.node.setTranslateY(node.getTranslateY() + (offsetY + (m1.vY * 4)));
return m1;
}
public void changeWeapon(KeyCode keyCode) {
this.keyCode = keyCode;
}
- public void shieldUp() {
+ public void shieldToggle() {
if (shield == null) {
@@ -444,7 +472,7 @@ public void handle(ActionEvent actionEvent) {
shieldOn = false;
flipBook.getChildren().remove(shield);
shieldFade.stop();
- collisionBounds = null;
+ collisionBounds = hitBounds;
}
})
.build();
@@ -459,7 +487,7 @@ public void handle(ActionEvent actionEvent) {
} else {
flipBook.getChildren().remove(shield);
shieldFade.stop();
- collisionBounds = null;
+ collisionBounds = hitBounds;
}

0 comments on commit 723eae9

Please sign in to comment.