• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Detecting collision from a user generated square with line segments.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically I have been asked to create a pinball game that fires a ball on to a board and the user can control flippers etc to keep the ball from hitting an absorber with added shapes to act as bumpers to keep the ball in play.

However, I've run in to a little problem with my collisions. The user can click a grid square on the board to highlight it and then use the 'Add Square' button to add a square of height 20 and width 20 to that highlighted square also returning its (x,y) position.


Once, this has been done I then add Line Segments to the square using the (x,y) coordinates from the use adding the square to prepare for collision detection with a ball.


..and here is the code for the collision detection when the ball hits either side of the square.


However, when I run the program the ball just carries on through the square. I think I am overlooking some key detail that I can't seem to solve which is why I'm here, so any help or pointers at all will be greatly appreciated.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading your code, it seems like you're basing a collision on time and velocity. That doesn't make a lot of sense to me. Wouldn't you base a collision on whether the area occupied by the ball overlapped with the area occupied by this other shape? When I did an interactive Python course on Coursera, we did a simulation of the Asteroids game as our final project and our spaceship had certain dimensions and asteroids had certain dimensions. To simplify things, we were told to just draw an imaginary circle around the Spaceship and the Asteroids. If any two circles overlapped, then we had a collision.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, your suggestion does sound much more plausible, however the specification says we need to base it on time and velocity so that when a ball hits a 'bumper' it reflects it properly with a new velocity and time. The ball reflects fine with the 4 walls of the game board but doesn't with the square's segments.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Well, you return a new CollisionDetails but I don't see where you're setting the ball's new velocity.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I might be mistaken, however, I thought this was setting the new velocity.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that's a variable and it seems to be outside of the ball. If you have ball.getVelocity(), it's likely that you have a corresponding ball.setVelocity(Velocity). Where do you "tell" the ball that it has to use newVelocity as its velocity now? How would that ball just know to use newVelocity if you don't tell it to?
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go back to your code that calculates collisions with a wall. Do you do anything with the ball, like say call a setVelocity method on it? If you do, you have to do the same thing when you collide with the square: tell the ball that it has a new velocity.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh okay I see, so I'll try and add in...


... and see if that helps any.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that and the ball is still carrying on through the square. I think there may be an issue with my board set up that might be throwing coordinates off or something because the wall collisions work fine with this code.

..and

But can I just say that I really appreciate you taking the time to continuously answer my questions and point out other things I seem to have missed.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Herther wrote:
But can I just say that I really appreciate you taking the time to continuously answer my questions and point out other things I seem to have missed.


Not a problem at all. Since your wall collision code looks the same, my next guess would be that this CollisionDetails object that gets returned is somehow used to update the ball. Do you have a ball.setCollisionDetails() method somewhere? If not, what do you do with the CollisionDetails object that gets returned? Does it get passed to the ball somehow? I'm guessing that somewhere in your code, there's something that looks like:
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With your permission of course, I could always either upload a zip file of my code or send it through another means so you can see exactly what I'm working with instead of me giving you a hard time trying to work out what I'm talking about and where certain lines of code are connected. It might also allow you to see exactly what happens when a square and a ball are added.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's kind of skirting the boundaries of protocol around here. Other folks have put their code on Github and provided links to it so anyone who's interested can take a look.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I shall do something along those lines and get back to you, if you don't mind?
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go for it, no problem.
 
Steven Herther
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, hopefully this link should work.
GitHub

I hope that is a clearer indication to how they classes are communicating. I included the board just in case you wanted to have a look at it as I mentioned earlier that it may be something in there that's causing the non-collision.

Oh and the RunMode class is where the collision detection code is written.

I hope this provides a greater insight to what I'm asking and I can't stress again how grateful I am.
 
Junilu Lacar
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took a quick look and I have to be honest, that's some pretty gnarly code. I suspected as much though and I pretty much called it:

The ball's velocity is being set from the velocity information from that CollisionDetails object, cd.

The timeUntilCollision method has multiple return statements all throughout and that's not a good thing to do. It makes following the flow of execution particularly difficult. The only thing I can suggest at this point is to sprinkle some System.out.println statements around the suspect area, particularly in that moveBall method. Print out what the ball's velocity is before you execute line 89, then print out what the ball's velocity is after line 91. At least you'll know what kind of values you're dealing with. I would think that the sign of x or y or both should change from positive to negative or vice versa when a collision happens.

Good luck.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic