Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Collision Detection

Posted on 2004-04-21
7
Medium Priority
?
321 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
  • 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
Industry Leaders: 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

Technology Partners: 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!

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 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