Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if actor is in contact with a block/actor group? #174

Open
illyabusigin opened this issue Mar 30, 2018 · 1 comment
Open

How to check if actor is in contact with a block/actor group? #174

illyabusigin opened this issue Mar 30, 2018 · 1 comment

Comments

@illyabusigin
Copy link

Hi John! I'm resuming work on my platformer and am trying to figure out the best way of determining if a user is in contact with a block/actor in a group.

My use-case is I have my player who will walk off the edge of an obstacle. When he walks off the edge I want to get a notification that he's no longer in contact with the ground so that I can apply gravity.

See GIF for my current behavior:
gravity

I've accomplished this by adding the following methods to Actor:

public func isInContact(withBlockGroup group: Group) -> Bool {
        for block in blocksInContact {
            if block.group == group {
                return true
            }
        }
        
        return false
    }
    
    public func isInContact(withActorGroup group: Group) -> Bool {
        for actor in actorsInContact {
            if actor.group == group {
                return true
            }
        }
        
        return false
    }

I have a plugin that watches for rectChanges and flips a flag which changes gravity behavior:

actor.events.rectChanged.addObserver(self) { (scene, player, stuff) in
            if !player.isInContact(withBlockGroup: self.floorGroup) {
                if self.isGrounded {
                    self.isGrounded = false
                }
            }
        }

Is there a better/more efficient way of doing this? If not I'd be happy to open a PR with the change.

@VNystad
Copy link

VNystad commented Jun 24, 2018

The way i've done this kind of collisiondetection is with a "2d"array, int[][]. It is done in c++, but i think it's somehow the same.

The size of 1 index in the array is 32*32 pixels, so the array coords for (24, 21) = [0][0] and (43,24) = [0][1]
I check if the array position below me has an value > 0 (a tile ID), if so, collision!
collidableArray[(int)(player.GetPosY()/ 32) + 1] [(int)player.GetPostX()/ 32] > 0, accessing array index O(1), so it's a really fast checkup

bool Physics::Grounded(Player* p, int** collidableArray)
{
// Variables for debugging
int playerArrayCoordX = (p->GetPosX() + 17) / 32;
int playerSouthCoord = ((p->GetPosY() + p->GetSizeHeight() / 2) / 32)>0 ? (p->GetPosY() + 50) / 32 : 0;
if (collidableArray[playerSouthCoord][playerArrayCoordX] > 0)
return true;
return false;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants