?
Solved

Collision Detection

Posted on 2004-04-21
7
Medium Priority
?
309 Views
Last Modified: 2011-10-03
Hello all, thanks for helping!

I'm currently programming a spaceship game, where 2 ships duke it out.
Each ship is an image, and the lasers that fly between them are fillRects.

Ok, ok, now to the problem. Both of my ships rotate a full 360 degrees, and are very rectangular in shape. This creates a huge problem for me when if comes to collision detection, or when the lasers hit the ship.

Is there any easier way to detect if a point is in a given region (even a circular region for that matter) with out having to make an impossibly long if statement?

I'm really looking for an easy way to see if a point has entered the area in which my image or other thing takes up.

Thanks!
0
Comment
Question by:Punkman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 

Author Comment

by:Punkman
ID: 10880947
Oh, this is currently in an applet, using BlueJ
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10881678
Well to see if a point (x1,y1) falls within a circle of radius r, centered at (x2,y2), just check if (x2-x1)^2 + (y2-y1)^2 <= r^2.

Do you need a formula for finding whether a point is within a rotated rectangle? Or are you looking for something much more automatic, like a method on an object that just figures it out for that object?
0
 
LVL 1

Accepted Solution

by:
manifoldronin earned 225 total points
ID: 10881715
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Punkman
ID: 10882239
@seanrowen Either is good, although I'm looking for the REAL easy way out. : )  

@manifoldronin This is the stuff I'm looking for a little more. But how exactly would I use this, with say, an image? Especially one that is constantly changing by rotation?
0
 
LVL 1

Expert Comment

by:manifoldronin
ID: 10889297
Each image has a bound rectangle, right?  And even though the image rotates constantly, at any given time (say the point you are drawing one frame).  So you basically have the abstraction of a rectangle(var ship).  Assuming one laser beam is a rectangle(var laser) as well, essentially you just need to test these conditions:
1. ship.contains(laser.x);
2. ship.contains(laser.y);
3. ship.contains(laser.x+laser.width);
4. ship.contains(laser.y+laser.height);
5. ship.contains(laser);

Any of those is true, you have a collision.
0
 

Author Comment

by:Punkman
ID: 10892902
Sorry, kind of new to Java >.<

How would I set up an area (or shape, confused about that...) so that I can check .contains() around (or of) my image?

ship is just an image, not an area or shape

Thank you for your help!
0
 

Author Comment

by:Punkman
ID: 10914520
nvm. I got it. Thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question