1

I'm designing a simple game where you're to avoid being hit by the AI i've created.

I've been looking up Collision detection and I have a question.

All the info i've looked up seem to involve using quite a bit of code.

I was wondering why can't a simple:

if(AI.xDirection == x || AI.yDirection == x || AI.xDirection == y || AI.yDirection == y){
        System.out.println("Collision");

Be used for this?

As you see I have it set to print to console and it seems to be working for me.

Is there a disadvantage to this?

3 Answers 3

2

Not sure what your code is actually doing, but if it works and is understandable to you, then I do not see why you should change it!

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. It doesn't make sense to me, but if it floats your boat, ship it.
Sorry!, the x and y is based on the coordinates i have for the object I move around the screen, and the AI.xDirection is a rectangle i have moving at random. I just looked up collision detection expecting the results to be as easy as i thought, and apparantly for me they are!
1

Your code assumes that all the client cares about is whether or not there is a collision. There may be games and other instances where the client wishes to deal with a collision in certain ways in certain situations (for example, I made a program a while ago that bounced a ball back and forth between random obstacles, and depending on which side was hit first, it had to bounce back in that direction).

The list goes on as to what the client wishes to happen after the collision, but if what you want is simply to show a collision, then hey, go for it man.

3 Comments

I get its not as simple as I have it, but I mean to initially detect a collision. So...essentially, using your example of the ball bouncing, couldnt I put the code to make it reflect off the objects into simple IF statement similiar to I'm using?
Yeah in the simplest of situations, what you have is correct. I'm just saying that you should be cognizant of the fact that for much more complex programs that deal with collision, the objects that are used cannot simply be translated into something that has an X and a Y variable, so there is much more code/logic involved
I get it, I just tested with some other shapes and I see where more advanced coding will be needed..Thanks for the help!
0

You violate encapsulation with this way of doing.

Prefer defines a custom object named for instance: AIPosition (likely to be immutable) dedicated to encapsulate AI coordinates.

and AIPosition containing the method: boolean doesCollideWith(AIPosition anotherPosition)

AI would contain a delegating method:

public boolean doesCollideWith(AI anotherAi){
    aiPosition.doesCollideWith(anotherAi.getAIPosition());
} 

Your call would be: if(ai1.doesCollideWith(ai2))

Thus, if later coordinates involve a third element (like z), your client codes don't need to change.

2 Comments

Yeah it was code like that I found when I looked it up. I'm still a Java novice and at first guess i came up with the if-statement i was using. Was just wondering why it wasn't more commonly used. thanks for the info!
@Sawyer05 Ok. So less conditional you have in your code the better. Why ? Check for the Open/Closed Principle en.wikipedia.org/wiki/Open/closed_principle and what is a really encapsulated code (not limited to getter and setter)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.