Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collision?

 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, been trying to add collision to my engine and I can't seem to find a very efficient method, any ideas?

Also, right now this is what i have (this method is called if i want to move things on the screen; x and y being the amount i want to move in each direction):



for some reason once two objects collide the program completely freezes and stops responding.

If there's a better way for me to be doing this, please let me know! Thanks.
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jake.

Apparently "this" object during your test is neither cwo obejct nor compare object. Moving "this" object does not affect any of that two colliding objects. So you move your object in endless loop.

Try to get a paper and write which objects your are comparing and how you want to move them. It may be not a simple task if there are many objects moving toward one point.

Also comparing two hashCodes looks suspicious because two different objects may have same hashCode and you may miss a collision.
 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maxim Karvonen wrote:Hi, Jake.

Apparently "this" object during your test is neither cwo obejct nor compare object. Moving "this" object does not affect any of that two colliding objects. So you move your object in endless loop.

Try to get a paper and write which objects your are comparing and how you want to move them. It may be not a simple task if there are many objects moving toward one point.

Also comparing two hashCodes looks suspicious because two different objects may have same hashCode and you may miss a collision.


Thanks for the reply,

The "this" object is actually an object that places all objects relative to the value of it.

Also, without hashcode, how can i make sure that im not comparing one to itself?
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make sure those two objects are not the same object you do this:



Was that what you meant?
 
Jake Cirino
Greenhorn
Posts: 17
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:To make sure those two objects are not the same object you do this:



Was that what you meant?

Yes, I'm not sure how .equals() would work with an object like that, but it works fine.

I managed to find the solution myself, when x and y were changing, all the of rectangles were not updated, so therefore creating a loop decreasing x and y when they weren't updating to their new positions after each loop resulted in an endless loop, thus causing the error.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic