1

I'm making my first free-movement (not tiles restricted) 2d game and was wondering how to work with the collision detection. I have 3 possible methods on my mind but not sure what would be the most efficient one:

  1. Each object on map has a texture smaller then the image itself (for example, a tree would have a smaller texture representing the base of it only) that can't collide. For this i would use a 2nd invisible texture to mark the area.

  2. Each object has a rectangle or circular area for the collision. Possible each texture would need a aux file with how big the collision should be (tree example again).

  3. I create a tiled map where the tiles are small enough as to let the player almost touch the object.

I would really appreciate if someone could help me decide what method I should be using x.x. thanks in advance.

(If you play old games, I'm trying to follow the Secret of Mana game movement style, if that helps)

1
  • I once made a table soccer game and I used circles to detect collisions. Given the nature of my game, this method was good enough to detect collisions. Simply checked whether the distance between the center of two circles was less than the sum of their radiuses. Really simple to do. However, if your objects have more complex shapes, this might not be a good solution. Commented Jul 23, 2014 at 23:03

2 Answers 2

0

Well you could make a class that stores the shape of the different entities and has methods for finding overlapping shapes. My answer is very broad, but this would be the best solution if what you are looking for is realistic collision detection. Circles or rectangles work as well but are far less realistic**

** Depends on your physics. If it is only a side scroller then it may not be as big a deal...

UPDATE:

By Shapes I am referring to an Object with Vertices, not a pixel-precision image of your entity!!!!!

Example EntityShape class:

public class YourEntityShape{

    private Point p1, p2, p3, p4, p5, p6;

    public YourEntityShape(Point p1, Poin...... etc.){
        this.p1 = p1;
        this.p2 = p2;
        //etc....
    }

    //Getter for private Points
    public Point getP1(){
        return p1;
    }
    //etc..

    public boolean overlaps(OtherEntityShape s){
        //Your collision detection code...
    }
}

As suggested by user 1337, you could use float[] arrays to store the x and y values.

example:

float[] x;
float[] y;

Then you can populate them with the corresponding x and y values of the points.

Sign up to request clarification or add additional context in comments.

9 Comments

It's a top view game, but ye I guess that is a possible solution.
@Monokilho Please do yourself a favour and take a bit of a different, dynamic (and faster!) approach and use a float[] points (in order x, y).
@TheJavaCoder16 Ye, with interactable and movable objects I won't be doing pixel-precision as that may be a bit too heavy, but I'll most likely use something similar for those on those objects.
@1337 I could yes, but if i wanted to do something "rough and fast" I could have done already so, unfortunately I'm trying to be sorta off a perfectionist on this project.
@Monokilho The float[] points approach is faster, so I am not sure what you are trying to tell me.
|
0

Why not a combination of those? Pixel perfect collision is rather expensive so you should do things in steps. If your world is large i'd add another step too by dividing your world into chunks.

First, when a chunk contains or intersects with a moving entity it starts checking for collisions within that chunk with cheap rectangles/radius. When there is a individual collision you can check for pixel perfect collision for this object.

Using these methods you make sure you are not calculating collision for objects far outside the screen and only do pixel perfect collision when things are really close.

1 Comment

chunks ain't a bad idea since I do plan to make a large outside world map for the player to explore.. and when you say pixel perfect collision, should I be doing that with fine tiles? or somehow outline almost perfectly the collision area?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.