Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Collision Detection using SVGPath.intersect

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Collision Detection using SVGPath.intersect

    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.

  2. #2
    Junior Member
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection using SVGPath.intersect

    So I eventually discovered the answer to my own question. I think typing it all out here helped me better understand the problem. For anyone who might come upon this that is also using this book...

    The code above is correct and works fine. The issue was the author of the book established two representations of the game pieces inside a high level abstract class 'Actor'. One is the spriteFrame (ImageView object containing the actual sprite image), the other is the spriteBound (SVGPath object containing SVG strings for pathing a non-visible collision polygon). When constructing the classes that would super Actor into more specific subclasses (Prop, Hero, etc), the author provides a method to .setTranslate of the spriteFrame object to make the character move, or place the prop in the correct position when instantiated. The author fails to also add a method to .setTranslate of the spriteBound object so that non-visual polygon "follows" the visual image of the sprite on screen.

    The result of this was basically the SVGPath objects both being instantiated at the 0,0 coordinates and that ultimately led to the static values I was pulling from getBoundsInLocal.getWidth(), because they were just being layered over one another and not moving. Hopefully this will help someone using this book down the road.

Similar Threads

  1. Collision Detection
    By Jobrien15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 17th, 2013, 09:00 PM
  2. Collision Detection
    By Jobrien15 in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 2nd, 2013, 09:46 AM
  3. Collision detection
    By Cutupangels in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2013, 08:48 PM
  4. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 11:39 AM