Made the hurt hazard work better.
This commit is contained in:
@ -175,6 +175,15 @@ Transform * Transform::getParent() {
|
||||
return this->parent;
|
||||
}
|
||||
|
||||
bool_t Transform::isChildOf(Transform *parent) {
|
||||
Transform *current = this->getParent();
|
||||
while(current != nullptr) {
|
||||
if(current == parent) return true;
|
||||
current = current->getParent();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Transform::~Transform() {
|
||||
this->setParent(nullptr);
|
||||
|
||||
|
@ -162,6 +162,15 @@ namespace Dawn {
|
||||
*/
|
||||
Transform * getParent();
|
||||
|
||||
/**
|
||||
* Returns true if this transform is a child of the given transform, this
|
||||
* climbs up the heirarchy until it finds a match.
|
||||
*
|
||||
* @param p Transform to check if this transform is a child of.
|
||||
* @return True if this transform is a child of the given transform.
|
||||
*/
|
||||
bool_t isChildOf(Transform *p);
|
||||
|
||||
/**
|
||||
* Dispose and clenaup this transform, also removes self from parent.
|
||||
*/
|
||||
|
@ -85,15 +85,13 @@ bool_t Dawn::boxIsBoxColliding(
|
||||
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB
|
||||
) {
|
||||
// Check for no overlap in X axis
|
||||
if (posA.x + maxA.x < posB.x + minB.x || posA.x + minA.x > posB.x + maxB.x) {
|
||||
if (posA.x + maxA.x < posB.x + minB.x || posB.x + maxB.x < posA.x + minA.x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check for no overlap in Y axis
|
||||
if (posA.y + maxA.y < posB.y + minB.y || posA.y + minA.y > posB.y + maxB.y) {
|
||||
if (posA.y + maxA.y < posB.y + minB.y || posB.y + maxB.y < posA.y + minA.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// There is overlap in both X and Y axis, so the boxes are colliding
|
||||
return true;
|
||||
}
|
13
src/dawn/physics/2d/Physics2D.hpp
Normal file
13
src/dawn/physics/2d/Physics2D.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
static inline glm::vec2 physics3Dto2D(glm::vec3 v) {
|
||||
return glm::vec2(v.x, v.z);
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
#include "Collider2D.hpp"
|
||||
#include "physics/2d/Box.hpp"
|
||||
#include "physics/2d/Physics2D.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class BoxCollider : public Collider2D {
|
||||
|
@ -16,10 +16,6 @@ void CharacterController2D::onStart() {
|
||||
useEvent([&](float_t delta){
|
||||
// Common variables
|
||||
auto myCollider = item->getComponent<Collider2D>();
|
||||
glm::vec2 currentPosition(
|
||||
this->transform->getLocalPosition().x,
|
||||
this->transform->getLocalPosition().z
|
||||
);
|
||||
|
||||
// Friction
|
||||
velocity -= velocity * friction * delta;
|
||||
@ -40,7 +36,7 @@ void CharacterController2D::onStart() {
|
||||
while(itColliders != allColliders.end()) {
|
||||
auto c = *itColliders;
|
||||
++itColliders;
|
||||
if(c->item == this->item) continue;
|
||||
if(c->item == this->item || c->transform->isChildOf(this->transform)) continue;
|
||||
result = c->getCollidingResult(
|
||||
velocity,
|
||||
myCollider,
|
||||
@ -66,10 +62,10 @@ void CharacterController2D::onStart() {
|
||||
}
|
||||
}
|
||||
|
||||
if(
|
||||
mathAbs<float_t>(moveAmount.x) <= 0.001f &&
|
||||
mathAbs<float_t>(moveAmount.y) <= 0.001f
|
||||
) return;
|
||||
// if(
|
||||
// mathAbs<float_t>(moveAmount.x) <= 0.001f &&
|
||||
// mathAbs<float_t>(moveAmount.y) <= 0.001f
|
||||
// ) return;
|
||||
|
||||
transform->setLocalPosition(
|
||||
transform->getLocalPosition() + (glm::vec3(moveAmount.x, 0, moveAmount.y) * delta)
|
||||
@ -81,7 +77,7 @@ void CharacterController2D::onStart() {
|
||||
while(itTriggers != allTriggers.end()) {
|
||||
auto c = *itTriggers;
|
||||
++itTriggers;
|
||||
if(c->item == this->item) continue;
|
||||
if(c->item == this->item || c->transform->isChildOf(this->transform)) continue;
|
||||
if(c->getCollidingResult(myCollider)) {
|
||||
c->eventTriggerEnter.invoke(this);
|
||||
}
|
||||
|
@ -29,8 +29,7 @@ bool_t SolidController2D::getCollidingResult(
|
||||
assertNotNull(movingObject);
|
||||
if(movement.x == 0 && movement.y == 0) return false;
|
||||
|
||||
auto localPos = movingObject->transform->getWorldPosition();
|
||||
glm::vec2 myPos(localPos.x, localPos.z);
|
||||
auto myPos = physics3Dto2D(movingObject->transform->getWorldPosition());
|
||||
|
||||
// Check what the moving object is
|
||||
switch(movingObject->getColliderType()) {
|
||||
@ -43,8 +42,7 @@ bool_t SolidController2D::getCollidingResult(
|
||||
case COLLIDER2D_TYPE_BOX: {
|
||||
auto box2 = dynamic_cast<BoxCollider*>(this->collider);
|
||||
assertNotNull(box2);
|
||||
auto localPos2 = box2->transform->getWorldPosition();
|
||||
glm::vec2 otherPos(localPos2.x, localPos2.z);
|
||||
auto otherPos = physics3Dto2D(box2->transform->getWorldPosition());
|
||||
|
||||
return boxCheckCollision(
|
||||
myPos, box1->min, box1->max,
|
||||
|
@ -32,8 +32,8 @@ bool_t TriggerController2D::getCollidingResult(Collider2D* movingObject) {
|
||||
auto box2 = dynamic_cast<BoxCollider*>(collider);
|
||||
assertNotNull(box2);
|
||||
return boxIsBoxColliding(
|
||||
box1->transform->getWorldPosition(), box1->min, box1->max,
|
||||
box2->transform->getWorldPosition(), box2->min, box2->max
|
||||
physics3Dto2D(box1->transform->getWorldPosition()), box1->min, box1->max,
|
||||
physics3Dto2D(box2->transform->getWorldPosition()), box2->min, box2->max
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,6 @@ namespace Dawn {
|
||||
class TriggerController2D : public SceneItemComponent {
|
||||
public:
|
||||
Collider2D *collider = nullptr;
|
||||
|
||||
StateEvent<CharacterController2D*> eventTriggerEnter;
|
||||
|
||||
TriggerController2D(SceneItem *i);
|
||||
|
Reference in New Issue
Block a user