Check if objects collide in 2d, without specifying which objects?

Started by
15 comments, last by jellyfishchris 10 years, 11 months ago

It's not a bad way, but you can improve it. Instead of adding an entityName, when you check collision you can verify the type of the obstacle instance:


//If there is an obstacle
if(obstacle != null) {
   if(obstacle instanceof Paddle) {
      this.invGoingLeft();
   }
	        
   if(obstacle instanceof Wall) {
      this.invGoingDown();
   }
}


Also in the second snippet of code your posted you have two if


if(obstacle.entityName == "Wall")

and


if(obstacle.entityName != "Wall") 


Consider using "else" instead of the second condition.
Also if you don't want to get lost in your code and follow standards go read about Indentation at http://en.wikipedia.org/wiki/Indent_style .

Is this a good way or would this be considered a bad way to check what it collides with?

I get that in more advanced games where there are more objects and factors etc this would probably be a bad way.



It is a simple game, I think this collision detection is good for a pong game, though if you start making bigger projects, you might consider more advanced techniques which will fits your needs. As for now it's very good to start somewhere less complicated to get more experience.
Advertisement

It's not a bad way, but you can improve it. Instead of adding an entityName, when you check collision you can verify the type of the obstacle instance:

I see, it's just that I read somewhere that (and I semi-quote it) "If you need to use instanceof, it means your code is bad/needs improvement" and many agreed on what that person said. I dunno though how true it is and I don't remember if it was in game-development or general programing (as these are different in my opinion).

What do you think?

Consider using "else" instead of the second condition.

Yes I normaly use that, but for some reason the program didn't like it. It might have been a bug though, I'm gonna try again :)

Also if you don't want to get lost in your code and follow standards go read about Indentation at http://en.wikipedia.org/wiki/Indent_style .

I will do that, thanks! :)

I see, it's just that I read somewhere that (and I semi-quote it) "If you need to use instanceof, it means your code is bad/needs improvement" and many agreed on what that person said. I dunno though how true it is and I don't remember if it was in game-development or general programing (as these are different in my opinion).

What do you think?



I don't think it's bad to use instanceof for this part of the code. Storing a string for the name would be perfect if it would be used as an unique Identifiant. Though,
all your paddles will be name "paddle" and all your walls will be named "wall". I think you must adjust your code with the functionnalities that you are looking for. Therefore, as you don't need 1000 different entities and you have to implement collision detection differently on each of your entity, it is a good way to do it.

I don't think it's bad to use instanceof for this part of the code. Storing a string for the name would be perfect if it would be used as an unique Identifiant. Though,
all your paddles will be name "paddle" and all your walls will be named "wall". I think you must adjust your code with the functionnalities that you are looking for. Therefore, as you don't need 1000 different entities and you have to implement collision detection differently on each of your entity, it is a good way to do it

I see, that seems reasonable!

Okay I've edited the collision so that it uses instanceof.

Though the collision is sometimes a bit buggy, for example: if the ball hits the top or bottom edge of the paddles it kinda goes into it and bounces inside until it gets out haha..

Not sure how to stop this though!

I find a cleaner solution to these sorts of problems is to give your Entity a reflection 'property' that you just multiply by the ball's velocity. This property or properties sets a value that applies the appropriate reflection to the colliding ball. There are only two values to consider, a value of 1 does not modify the velocity while a value of -1 reflects it.

So, some pseudo code (as I'm not a java programmer)


if(ObjA collides with Ball)
{
	Ball.xVelocity *= ObjA.xReflection;
	Ball.yVelocity *= ObjA.yReflection;
}

Set ObjAs x and y reflection to 1 and -1, and you get reflection appropriate for a horizontal wall (y velocity is reflected, but x velocity remains unchanged).

Set it to -1 and 1, and you get reflection appropriate to a vertical paddle.

The code doesn't really care what type of Object it is working with. There aren't any additional conditions beyond the test for collision. You aren't writing separate methods for each collision case. If the objects have collided, apply the reflection to the balls velocity.

Personally if I just want to do "simple" collision checking without regards to spatial comparisons, I just make a like "world" object and add all the entities to that, and have it manage it. A bonus to that method is you can just run a collisioncheck method on it and have it automatically check each object against each other(since the world would contain a list of them) and call something like HandleCollision(target) on each entity that's colliding.

This lets you do things like make walls change sprites if they're collided with, or nothing at all if you prefer. Of course the ball has a reference to whatever it collided with so it can compare itself to the object and figure out which velocity to reverse along with shifting it out of the other object's bounding box.

The problem your coming into is your trying to merge collision detection (Maths), and collision resolution (game logic).
And you want this to be eligate, you might have to think your design a little bit and have a detection facility and this is then passed to the resolution section to handle how a ball hits a wall for example!

Hope this helps

This topic is closed to new replies.

Advertisement