From b827885f402f23ce1e2c994d65ee3f2433e06f04 Mon Sep 17 00:00:00 2001 From: Remco Date: Fri, 3 Jun 2016 13:51:54 +0200 Subject: [PATCH 1/2] enemy collide logic in enemy and out of world --- Enemy.cpp | 9 +++++++-- Enemy.h | 2 +- World.cpp | 9 +++------ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Enemy.cpp b/Enemy.cpp index 2864fb8..ee3134a 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -59,9 +59,14 @@ void Enemy::inEyeSight(Vec3f & TargetPosition) hasTarget = false; } -bool Enemy::hasCollison(Vec3f &) +void Enemy::collide(const Entity * entity) { - + Vec3f difference = position - entity->position; //zou misschien omgedraait moeten worden + difference.y = 0; + difference.Normalize(); + difference = difference * (entity->model->radius + 0.01f); + position.x = difference.x + entity->position.x; + position.z = difference.z + entity->position.z; } void Enemy::update(float delta) diff --git a/Enemy.h b/Enemy.h index 44aec67..e0cabd7 100644 --- a/Enemy.h +++ b/Enemy.h @@ -18,6 +18,6 @@ public: void draw(); void inEyeSight(Vec3f &); - bool hasCollison(Vec3f &); + void collide(const Entity *entity); }; diff --git a/World.cpp b/World.cpp index 57a5347..5af2196 100644 --- a/World.cpp +++ b/World.cpp @@ -168,7 +168,7 @@ void World::update(float elapsedTime) //Al deze code zou in enemy moeten staan enemy->inEyeSight(player->position); - + enemy->update(elapsedTime); if (enemy->hasTarget) { @@ -176,14 +176,11 @@ void World::update(float elapsedTime) { if (e->canCollide && e->inObject(enemy->position)) { - Vec3f difference = e->position - enemy->position; //zou misschien omgedraait moeten worden - difference.Normalize(); - difference = difference * (e->model->radius + 0.01f); - enemy->position = e->position + difference; + enemy->collide(e); break; } } - } + } //tot hier } } From 293ceb0b9fc43e9ea6221323c1616f5b6bc85d22 Mon Sep 17 00:00:00 2001 From: Remco Date: Fri, 3 Jun 2016 13:58:07 +0200 Subject: [PATCH 2/2] enemy walking around world --- Enemy.cpp | 2 +- World.cpp | 3 +++ worlds/small.json | 10 ++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Enemy.cpp b/Enemy.cpp index ee3134a..9d77229 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -80,7 +80,7 @@ void Enemy::update(float delta) dz = target.z - position.z; length = sqrt(dx*dx + dz*dz); - if (length > 0.03) + if (length > 1) { dx /= length; dz /= length; diff --git a/World.cpp b/World.cpp index 5af2196..5faf4dc 100644 --- a/World.cpp +++ b/World.cpp @@ -6,6 +6,7 @@ #include "CrystalPoint.h" #include #include +#include "WorldHandler.h" World::World(const std::string &fileName) { @@ -181,6 +182,8 @@ void World::update(float elapsedTime) } } } + WorldHandler* worldhandler = WorldHandler::getInstance(); + enemy->position.y = worldhandler->getHeight(enemy->position.x, enemy->position.z) + 2.0f; //tot hier } } diff --git a/worlds/small.json b/worlds/small.json index 466db7c..b46e927 100644 --- a/worlds/small.json +++ b/worlds/small.json @@ -5,7 +5,7 @@ { "color":100, "file": "models/boom/Boom.obj", - "collision": false + "collision": true } ] }, @@ -13,5 +13,11 @@ "startposition": [ 20, 5, 20 ] }, "objects": [], - "enemies": [] + "enemies": [ + { + "file": "models/squid/Blooper.obj", + "pos": [ 20, 5, 10 ], + "scale": 0.01 + } + ] } \ No newline at end of file