• Post Reply Bookmark Topic Watch Topic
  • New Topic

Rectangles and collision detection  RSS feed

 
Bod MacNeil
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
 
Carey Brown
Saloon Keeper
Posts: 3310
46
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. 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!