Solved

Collision Detection

Posted on 2004-04-21
7
284 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 75 total points
ID: 10881715
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

803 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