Solved

Collision Detection

Posted on 2004-04-21
7
298 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 75 total points
ID: 10881715
0
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!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: anonymous class 4 53
passing enum to a method 4 48
batch vs regular insert in spring DAO 2 50
What is the use of Forwarding Class in java 1 52
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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