Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By kneitzel
  • 1 Post By gimbal2

Thread: Collision detection

  1. #1
    NewCsStudent is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Collision detection

    Hello Everybody!
    I am new to computer science and I enjoy this topic a lot. I use eclipse as my IDE. I have decided to make my own video game and somebody had sent me a class that could be used for collision detection. The problem with the collision detection class was that it made a box around an object and if something else had touched the object, the collision detection would work. My problem with this class is what happens when i want to use circles? I cant have a box drawn around it. Then it wouldn't work as i would want. Is there a pixel perfect collision detection class out there I can use? Thanks so much to everyone who helps me out! It'll be useful in my journey to become successful in computer science! By the way this is in the java language. My video game is being made as an applet also.
    Last edited by NewCsStudent; 04-30-2014 at 07:15 AM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Collision detection

    Hi,

    first of all: It is a common procedure to use boxes around objects. That way you can quickly determine if there might be an collision.
    If the box algorithm found a collision, then you can do further checks e.g. based on the used object itself.

    Mastering computer science is not about copying code. One of the most important thing in computer science is the ability to research something. You have to fully understand the requirements and how things work together so you can translate stuff into business rules and code. This also means that you need to think about the stuff behind the application. So for a business application you really have to understand the business or in your case you should do a research on mathematic topics.
    So it is your decision what you implement and how you want to implement it. One possibility is to calculate which areas are overlapping and then do a pixel by pixel check to see if 2 non transparent pixels are overlapping.
    But you could also choose to use easier to calculate with shapes and then check if they are overlapping or not.

    So my hint is that you simply research this. A simple google search on "collision detection based on pixels" could be a good start. But stop searching for classes or java code! First try to understand what is done how and why! (When you develop some piece of software, then you are responsible for it. How can you be responsible if you just copied and pasted code? If someone complains then you simply tell them "But java-forums.org provided that code"?)

    Konrad
    gimbal2 likes this.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    6

    Default Re: Collision detection

    I completely agree with kneitzel that "one of the most important thing in computer science is the ability to research something," and being able to do effective Internet searches is one of those things. E.g., I used the search term "java circle collision detection", and within a few minutes I found excellent reading material that goes into the maths and physics (I hope you've been paying attention to these subjects in class!) behind collision detection, as well as the algorithm and sample code to go with them. Some of the pages I found:

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Collision detection

    One must be in the frame of mind to be looking for understanding and not only code before Google can really be of help as a learning tool.
    kneitzel likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Collision detection

    This a an excellent example of playing around with a single aspect of a larger project until it works.
    If you are going to incorporate collision detection into a game, then I suggest you do something like the following.

    1. Create two images (stars, trapezoids, figures, etc).
    2. Learn how to move and rotate them. (Even if you don't used it in your game it will be useful to know).
    3. Now simply place one image fixed in the center of a panel and use the mouse to move and rotate the other.
    4. As the images begin to touch, see if your collision detection algorithm is working. For example, two stars may overlap
    such that their bounds (min (x,y) and max(x,y)) overlap but the actual stars don't. That is what you need to test for.
    5. Once you get the collision part worked out, then you don't have to worry about it when you integrate it into your final app.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Collision detection

    Quote Originally Posted by jim829 View Post
    This a an excellent example of playing around with a single aspect of a larger project until it works.
    If you are going to incorporate collision detection into a game, then I suggest you do something like the following.

    1. Create two images (stars, trapezoids, figures, etc).
    2. Learn how to move and rotate them. (Even if you don't used it in your game it will be useful to know).
    3. Now simply place one image fixed in the center of a panel and use the mouse to move and rotate the other.
    4. As the images begin to touch, see if your collision detection algorithm is working. For example, two stars may overlap
    such that their bounds (min (x,y) and max(x,y)) overlap but the actual stars don't. That is what you need to test for.
    5. Once you get the collision part worked out, then you don't have to worry about it when you integrate it into your final app.

    Regards,
    Jim
    Prototyping ftw!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    NewCsStudent is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Collision detection

    Hello Konrad,

    I thank you so much for answering me with such a descriptive answer. I'm a new student to computer science and I am an underclass-men in high school. I've only been studying CS for 6 months. I am curious to know if it's still possible for me to create this collision detection class with my low amount of knowledge. When I read the code other people write for the collision detection on google, I have no clue what's going on. Do you still think I could be able to create the pixel perfect collision detection class?

    Thank You,
    Kyle

  8. #8
    NewCsStudent is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: Collision detection

    Hello Konrad,

    I thank you so much for answering me with such a descriptive answer. I'm a new student to computer science and I am an underclass-men in high school. I've only been studying CS for 6 months. I am curious to know if it's still possible for me to create this collision detection class with my low amount of knowledge. When I read the code other people write for the collision detection on google, I have no clue what's going on. Do you still think I could be able to create the pixel perfect collision detection class?

    Thank You,
    Kyle

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Collision detection

    Hi,

    yes it is possible. But you have to research it. The logic behind collision detection is not that complex. It can get more complex if you try to handle image rotation and such things inside the same code but if you keep stuff separated, then it is not that hard.
    But I would always start more simple. So either by circle based collision detection (which is really straight forward. 2 circles of radius r1 and r2 touch or overlap if the distance is <= r1+r2. So if you draw it down on paper you found the mathematics quite simple because it is a triangle with a 90° - so you get some logic like SQRT((x1-x2)^2 + (y1-y2)^2) <= r1+r2. And if you have integers (full pixels only) you can get rid of the SQRT operation with (x1-x2)^2 + (y1-y2)^2 <= (r1+r2)^2.
    And if you want to start easy, then you make sure that the radius is always the same. So you end up with a one time calculation of 4*r^2.

    First implement this and work with this. When you get it up and running, then you can extend your code so that you check the pixels when the circles hit.

    This is important in my eyes because that way you can still have your progress and you simply do not stop at the first hard point (where you should know now, that a solution should be possible).

    When you want to dig into it, you could do a search for "2d pixel collision detection java" - but maybe you want to leave out the java and look if you find some nice words like "basics" or so. The important point is that you learn the principles behind. So you should not be looking for java code but for a description what an algorithm should do. Once you know these basics, it shouldn't be that hard to understand the code for it. You might even check if there are some nice books about this issue. Maybe you can get such a book at your high school. (In germany we have a system where you can borrow books. Even books that are not available in your location can be ordered for small money. Or if you are studying CS, then you might want to read a lot of books so maybe a subscription (e.g. safaribooksonline, books24x7 seems to be company focused only but there might even be more. Amazon Prime does not have a lot of computer books so that is crap in my eyes.) is a good way to go. (I pay 10 US$/m for a 5 slot bookshelf at oreily/safaribooksonline and it is great. Just be sure what books you add to your shelf because they stay there for 30 days.)

    Important is:
    Yes, you can do it! I would even suggest that you do that as a great training. Maybe you never ever have to write such an algorithm again, but it is a good opportunity to do a research. That is one of the most important things in my eyes. It is a daily part of my job to do researches. And when you build real products then you really have to research topics just to be sure that you build your stuff properly. And when you have problems understanding something: Feel free to ask. The forum can be great if you didn't understand specific parts. But it is quite bad to give you introductions or tutorials. (That is simply to much for a forum. If I could quickly write tutorials then my homepage wouldn't be a piece of crap with almost no content but a tutorial heaven :) But I am simply missing the time to do that...

    With kind regards,

    Konrad

Similar Threads

  1. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 01:45 PM
  2. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 04:29 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-08-2011, 12:40 AM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 05:55 PM
  5. Collision Detection
    By dotabyss in forum Java Gaming
    Replies: 0
    Last Post: 03-14-2010, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •