Skip to content

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

Description

@illyabusigin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions