Jump to content

Regarding entity movement


Undac
 Share

Recommended Posts

Hello there, I have 2 questions and any help / idea is welcome.

 

1. How can I make sure that the camera does not go off terrain?

2. I need to move the camera forward (0Z) and sidewards (0X) along it's own axes, but without taking in consideration the rotation around 0Y axis (up/down). Any idea? I tried to make some pictures, hope they help:

 

ICNxxor.png

 

and an example:

 

Wxuam08.png

 

 

PS: I have solutions for both problems, but I am not satisfied with them.

1. "trap" the camera inside a cube

2. increment the camera position with sin(y rotation) on 0X, and with cos(y rotation) on 0Z

 

code below:

 

 

I actually have a MiscareCamera class that extends App, but I tried to move the relevant methods in the App class.

 

App.cpp

scroll to MiscareCamera()

don't forget to load a map!

 

 

#include "App.h"

 

//throws this exception if the entity is not inside the box

class exceptie{

const char* ce() const throw(){

return "Camera in afara formei!";

}

}Afara;

 

App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}

 

App::~App() { delete world; delete window; }

 

bool App::Start(){

window = Leadwerks::Window::Create();

context = Leadwerks::Context::Create(window);

world = Leadwerks::World::Create();

camera = Leadwerks::Camera::Create();

 

//LOAD A MAP!!

if (!Leadwerks::Map::Load("/Maps/start.map")){

context->SetBlendMode(Leadwerks::Blend::Alpha);

context->DrawText("Failed to load a map! ", 2, 2);

context->Sync();

Sleep(1000);

}

 

camera->SetPosition(0, 30, 0);

return 1;

}

 

bool App::Loop(){

while (!(window->Closed() || window->KeyDown(Leadwerks::Key::Escape))){

App::MiscareCamera();

if (window->KeyDown(Leadwerks::Key::P)){

Sleep(200);

while (!(window->KeyDown(Leadwerks::Key::P))){

}

Sleep(200);

}

Leadwerks::Time::Update();

world->Update();

world->Render();

 

context->SetBlendMode(Leadwerks::Blend::Alpha);

context->DrawText("Rotatie: " + camera->GetRotation().ToString(), 2, 2);

context->DrawText("Pozitie: " + camera->GetPosition().ToString(), 2, 20);

context->Sync();

}

 

return 0;

}

 

void App::MiscareCamera(){

if (window->KeyDown(Leadwerks::Key::W)){

Leadwerks::Vec3 coordonate = camera->GetPosition();

try{

Leadwerks::Vec3 unghi = camera->GetRotation();

camera->SetPosition(coordonate.x + Leadwerks::Math::Sin(unghi.y), coordonate.y, coordonate.z + Leadwerks::Math::Cos(unghi.y));

inDreptunghi(camera->GetPosition());

}

catch (exceptie){

camera->SetPosition(coordonate.x, coordonate.y, coordonate.z);

}

}

 

if (window->KeyDown(Leadwerks::Key::S)){

Leadwerks::Vec3 coordonate = camera->GetPosition();

try{

Leadwerks::Vec3 unghi = camera->GetRotation();

camera->SetPosition(coordonate.x - Leadwerks::Math::Sin(unghi.y), coordonate.y, coordonate.z - Leadwerks::Math::Cos(unghi.y));

inDreptunghi(camera->GetPosition());

}

catch (exceptie){

camera->SetPosition(coordonate.x, coordonate.y, coordonate.z);

}

}

 

if (window->KeyDown(Leadwerks::Key::D)){

try{

camera->Move(1, 0, 0);

inDreptunghi(camera->GetPosition());

}

catch (exceptie){

camera->Move(-1, 0, 0);

}

}

 

if (window->KeyDown(Leadwerks::Key::A)){

try{

camera->Move(-1, 0, 0);

inDreptunghi(camera->GetPosition());

}

catch (exceptie){

camera->Move(1, 0, 0);

}

}

 

if (window->KeyDown(Leadwerks::Key::R) && camera->GetPosition().y < 81){

camera->Move(0, 1, 0);

}

 

if (window->KeyDown(Leadwerks::Key::F) && camera->GetPosition().y > 34){

camera->Move(0, -1, 0);

}

 

if (window->KeyDown(Leadwerks::Key::E)){

Leadwerks::Vec3 unghi = camera->GetRotation();

camera->SetRotation(unghi.x, unghi.y + 2, 0);

}

 

if (window->KeyDown(Leadwerks::Key::Q)){

Leadwerks::Vec3 unghi = camera->GetRotation();

camera->SetRotation(unghi.x, unghi.y - 2, 0);

}

 

if (window->KeyDown(Leadwerks::Key::Z)){

Leadwerks::Vec3 unghi = camera->GetRotation();

if (unghi.x<30)

camera->SetRotation(unghi.x + 1, unghi.y, 0);

}

 

if (window->KeyDown(Leadwerks::Key::C)){

Leadwerks::Vec3 unghi = camera->GetRotation();

if (unghi.x>0)

camera->SetRotation(unghi.x - 1, unghi.y, 0);

}

}

 

void App::inDreptunghi(Leadwerks::Vec3 Obiect){

int xMin = -90, xMax = 90, zMin = -90, zMax = 90;

if (!(Obiect.x >= xMin && Obiect.x <= xMax))

throw Afara;

if (!(Obiect.z >= zMin && Obiect.z <= zMax))

throw Afara;

}

 

 

 

App.h

 

 

#pragma once

 

#include "Leadwerks.h"

 

#undef GetFileType

 

class App{

public:

Leadwerks::Window* window;

Leadwerks::Context* context;

Leadwerks::World* world;

 

App();

virtual ~App();

 

bool Start();

bool Loop();

 

private:

//mutate din MutareCamera.h

Leadwerks::Camera *camera;

void MiscareCamera();

void inDreptunghi(Leadwerks::Vec3 Obiect);

};

 

 

 

 

 

 

PPS: It didn't take the whitespaces when I copy-pasted from VS. Any idea how to solve this?

PSPS: I posted the camera movement method on pastebin: http://pastebin.com/BLBQgjAj

Edited by Undac
Link to comment
Share on other sites

The first one is tricky, but the second can be done fairly easily. I don't have access to Leadwerks right now, so I can't code anything that works, but I can try to explain it.

1. Get your local forward movement direction, including the y axis

2. Set y to 0

3. Normalize your movement vector

4. Multiply the vector by your movement speed

5. Apply the movement

 

If you don't understand what I'm trying to explain, just let me know and I'll whip up the code in about 8 or so hours.

  • Upvote 1
Link to comment
Share on other sites

The first one is tricky, but the second can be done fairly easily. I don't have access to Leadwerks right now, so I can't code anything that works, but I can try to explain it.

1. Get your local forward movement direction, including the y axis

2. Set y to 0

3. Normalizeyour movement vector

4. Multiply the vector by your movement speed

5. Apply the movement

 

If you don't understand what I'm trying to explain, just let me know and I'll whip up the code in about 8 or so hours.

 

Just tried to do this myself but I keep messing up somewhere, could you show us an example of something simple?

Link to comment
Share on other sites

Ok, I thought that there is a method similar to Entity::Move which allows us to ignore one of the rotations.

 

ixFirebal69xl - I cannot test right now, but I think that it's something like:

if (window->KeyDown(Leadwerks::Key::W)){

Leadwerks::Vec3 normalizedVector = camera->GetPosition();

normalizedVector.y = 0;

normalizedVector.Normalize(); // it sais that the function returns a Vec3, so it might be normalizedVector = normalizedVector.Normalize()

camera->Move(normalizedVector);

}

 

In case I understood it wrong and it does not work, you can use my solution meanwhile:

if (window->KeyDown(Leadwerks::Key::J)){

camera->SetPosition(camera->GetPosition().x + Leadwerks::Math::Sin(camera->GetRotation().y), camera->GetPosition().y, camera->GetPosition().z + Leadwerks::Math::Cos(camera->GetRotation().y));

}

Link to comment
Share on other sites

Just tried to do this myself but I keep messing up somewhere, could you show us an example of something simple?

 

I tried to code up an example, but in doing do, I decided to just write an entire RTS camera that should do what you want it to do.

You can get it here http://steamcommunity.com/sharedfiles/filedetails/?id=617085158

(It may not be available yet, as I only just submitted it)

 

Have a look at the script and see how to do it, or you may find that it does what you want it to do.

If it is missing something, just let me know (As long as it's not too complicated)

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...