user1150769
user1150769

Reputation: 395

Collision detection on a rotated Image

How would you go about detecting collision on a rotated Image in a game? I am making an asteroids game an I cannot figure out how to make the asteroids properly collide with the rotated spaceship.

Upvotes: 1

Views: 673

Answers (4)

PoxelColl
PoxelColl

Reputation: 301

Pixel-perfect collision detection, ie. collision detection between images, is generally really hard to do efficiently. For that reason, it is generally a good idea to use an existing, optimized library built for that purpose.

Since you also need support for rotated images, I recommend PoxelColl. It supports pixel-perfect collision detection as well as basic transformations such as scaling and rotation. It provides a Scala port, which is compatible with Java.

Upvotes: 1

XiaoChuan Yu
XiaoChuan Yu

Reputation: 4011

What exactly are you using for collision boundaries of your asteroid?

Simplest might be that you can just use circles for everything and implement a circle-circle collision detection (just google it). This may not visually pleasing if your images are not very circle like.

Otherwise, if you have rotating rectangles colliding with other rotating rectangles then you'll have to implement an algorithm using Separating Axis Theorem for 2D Rotated Rectangle Collision.

Another option might be pixel perfect collision detection which is what Chuck was talking about. A quick search turned up this forum post. Proceed with caution though, this method's performance degrades with the size of your images.

Upvotes: 0

Java42
Java42

Reputation: 7706

In paint(), as you prepare to draw the in-motion image, check the pixel colors of the destination points and look for the target object's color(s). The in-motion image and the target object must be, of course, different colors.

Upvotes: 1

trashgod
trashgod

Reputation: 205785

If the rotated object is one that implements the Shape interface, it may have a useful implementation of the contains() method.

Upvotes: 2

Related Questions