Hi
Did some more testing and now it is working as I wanted. Had to use :
auto pick = world->Pick(sphere->GetPosition(), Vec3(sphere->GetPosition().x, lowest, sphere->GetPosition().z), 0.0f, true);
It was the radius parameter I got wrong. Set it to expected max value but it looks like it works with 0.0f. Instead of using the pick.entity.Position it worked perfect using the pick.position (which looks to be the location of the casted ray entering the entity found.
The whole test program looks like this :
#include <string>
#include "UltraEngine.h"
#include "ComponentSystem.h"
using namespace UltraEngine;
#define SPHERE_SIZE 0.5f
#define SPHERE_MOVE_FORCE 10.0f
#define SPHERE_JUMP_FORCE 500.0f
int main(int argc, const char* argv[])
{
//Get the displays
auto displays = GetDisplays();
//Create a window
auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
//Create a world
auto world = CreateWorld();
//Create a framebuffer
auto framebuffer = CreateFramebuffer(window);
//Create a camera
auto camera = CreateCamera(world);
camera->SetClearColor(0.125);
camera->SetPosition(0, 7, 2);
// Load map
auto scene = LoadScene(world, "Maps/new.map");
//Create sphere
auto sphere = CreateSphere(world, SPHERE_SIZE);
sphere->SetColor(1, 0, 0);
sphere->SetPosition(2, 10, 2);
sphere->SetMass(1);
// Get z-position to put camera on and minimal y value of scene
float closest = 999.0f;
float lowest = -999.0f;
for (auto current : scene->entities) {
auto aabb = current->GetBounds();
if (aabb.min.z < closest) closest = aabb.min.z;
if (aabb.min.y < lowest) lowest = aabb.min.z;
}
float distanceToEntity = 0.0f;
//Main loop
while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
{
// check if sphere is in the air
bool airborne = false;
auto pick = world->Pick(sphere->GetPosition(), Vec3(sphere->GetPosition().x, lowest, sphere->GetPosition().z), 0.0f, true);
if (pick.success) {
distanceToEntity = sphere->GetPosition().y - pick.position.y;
}
if (distanceToEntity > (SPHERE_SIZE + 0.1f)) {
airborne = true;
}
// Adding torque can be done while in the air causing a spin effect on contact :-)
// Add torque to the Z-axis (moving along the x-axis)
if (window->KeyDown(KEY_A)) {
sphere->AddTorque(Vec3(0.0f, 0.0f, SPHERE_MOVE_FORCE));
}
if (window->KeyDown(KEY_D)) {
sphere->AddTorque(Vec3(0.0f, 0.0f, -SPHERE_MOVE_FORCE));
}
// Add torque to the X-axis (moving along the z-axis)
if (window->KeyDown(KEY_S)) {
sphere->AddTorque(Vec3(-SPHERE_MOVE_FORCE, 0.0f, 0.0f));
}
if (window->KeyDown(KEY_W)) {
sphere->AddTorque(Vec3(SPHERE_MOVE_FORCE, 0.0f, 0.0f));
}
if (!airborne) {
if (window->KeyHit(KEY_SPACE)) {
sphere->AddForce(Vec3(0.0f, SPHERE_JUMP_FORCE, 0.0f));
}
}
world->Update();
camera->SetPosition(sphere->GetPosition().x, 7.0f, closest);
camera->Point(sphere);
world->Render(framebuffer);
}
return 0;
}
Using a map (new.map) that looks like this:
Rolling up the ramp had some issues when using the entity's position. Using the PickInfo's position worked well.
BR
Eirik