Solved

Collision Detection

Posted on 2004-04-21
7
265 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
Comment Utility
Oh, this is currently in an applet, using BlueJ
0
 
LVL 1

Expert Comment

by:seanrowen
Comment Utility
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
Comment Utility
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Punkman
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
nvm. I got it. Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now