Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Rectangles and collision detection  RSS feed

 
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I've made a small program which has shark object moving across the screen.  I want detect when the sharks touch the submarine object at the other side.  I have placed a rectangle around both objects but when I run the program its a bit buggy.  The texture no longer shows for either object and sometimes only 1 rectangle shows.  Collision detection doesn't work at all.  I've posted my code below in the hopes that someone can spot something.  thanks. 

//drawShark() method


//spriteBatch inside the render() method


//WorldUtils class that both Shark class and SUbmarine class extend
 
Bartender
Posts: 4521
50
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the data type for 'sharks'?

What is Vector2?

Why do you maintain a position and size when you already have bounds?
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey, I should have mentioned that I was using the framework LibGDX.  It has a built in Vector2 class, which has an x,y coordinate system for making games.  Its makes moving objects a lot simpler.   sharks is an ArrayLisst is of type Shark, which  is a class I made.  Here is the class:




I thought I had to maintain a position so I can set the bounds to that position.  I'm also updating the position every frame.  I got the bounds.intersects() working earlier but I did something to stop it again.  I think it will just take some fiddling around with.
 
Bod MacNeil
Ranch Hand
Posts: 62
2
Java Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SOLVED!  I changed the method to get the instead of .  This took me a couple of days but glad I got it in the end.  My code isn't perfect so no doubt there will be more problems on the way. 
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!