JohnSundell / JohnSundell/ImagineEngine

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

Open
#174 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
1.8k
Forks
104
PR merge metrics
No merged PRs in 30d

Description

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](https://user-images.githubusercontent.com/814431/38156242-1c189846-3442-11e8-9690-000622ff4e67.gif)

I've accomplished this by adding the following methods to Actor:
```swift
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:
```swift
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.