Let me preface my question by stating I'm currently using the book "Beginning Java 8 Games Development" as a guide to build a small 2D demo game engine, but I've branched off a bit from the build described in the book for my own purposes while using the book as a template for how to structure the code and how to approach certain concepts. That has worked out well so far, but I find myself struggling with collision detection.
To the heart of the issue: I'm attempting to use two levels of collision detection, a course detection using the getBoundsInParent.intersects of two ImageView (containing my sprites) objects, and once that is detected to create a Shape object using SVGPath.intersect while feeding two SVGPaths (representing fine tuned sprite bounds as path data) and test getBoundsInLocal.getWidth of the resulting Shape to see if they actually collided and created a non-null shape with the intersect method. I'm using a Group Node at the top level, and my understanding is that the intersect method should test those paths against the coordinate space of the parent node, in this case the Group node 'root' at the top of my node stack. I have the collision linked to a sound that will play when it occurs.
The first layer of collision detection occurs without issue. When my 'hero' ImageView is moved (translated) into proximity of my 'barrel' ImageView, the code kicks off once the Bounds overlap. The nested statement underneath that immediately runs SVGPath.intersect to a new Shape and tests, but it appears the intersect method always returns non-null, and moreover always with the exact same number in .getWidth regardless of where my hero ImageView object is translated in relation to the Group parent node. I'm not sure where I'm tripping up in my logic thinking this through, but my initial debugging suggests that the ImageView translation is not being considered when testing the SVGPath data, as the resulting value remains static.
Collision code below:
@Override public boolean collide(Actor object) { boolean collisionDetect = false; double test = 0; // variable to debug .getWidth value if (arenaDemo.iHeroFighter.spriteFrame.getBoundsInParent().intersects(object.getSpriteFrame().getBoundsInParent())) { Shape intersection = SVGPath.intersect(arenaDemo.iHeroFighter.getSpriteBound(), object.getSpriteBound()); test = intersection.getBoundsInLocal().getWidth(); if (intersection.getBoundsInLocal().getWidth() != -1) { collisionDetect = true; } } return collisionDetect; } public void checkCollision() { for (int i = 0; i < arenaDemo.castMonitor.getCurrentCast().size(); i++) { Actor object = arenaDemo.castMonitor.getCurrentCast().get(i); if (collide(object)) { if (arenaDemo.checkAudioClip2() == true) {//Do Nothing } else {arenaDemo.playAudioClip2();} } else {arenaDemo.stopAudioClip2(); } } }
Curious if I'm overlooking something obvious in this code section, or if I need to look at some of my other classes for the problem. Thanks in advance for any assistance or guidance.