How can I make this in fewer lines?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
AlexAzazel
Posts: 31
Joined: Sun Jan 10, 2016 3:42 pm
Location: Tbilisi

How can I make this in fewer lines?

Post by AlexAzazel »

This is the first thing I wrote by myself and it works to my surprise. My goal was to move camera on horizontal plane along some particular vector2d<f32> direction; with W A S D. I did it like this

Code: Select all

 
if (receiver->IsKeyDown(KEY_KEY_W))
{
    core::vector3df camPos = camera->getPosition();
    core::vector3df camTar = camera->getTarget();
    const f32 Xupdate = (f32)speed * deltaTime*direction.X;
    const f32 Yupdate = (f32)speed * deltaTime*direction.Y;
    camPos.X += Xupdate; camPos.Z += Yupdate;
    camTar.X += Xupdate; camTar.Z += Yupdate;
    camera->setPosition(camPos);
    camera->setTarget(camTar);
}
 
I did the corresponding thing for A S and D too but it's irrelevant for now I think.

Is this the best way to do this or can it be simplified in terms of code or optimized in terms of speed/memory?

And one side question, why did I have to update camPos.Z and camTar.Z instead of .Y for it to work?
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How can I make this in fewer lines?

Post by CuteAlien »

Looks ok to me. It's Z because Y is probably up. If you want it along x/y instead then make sure the camera target has the same z as camera position and differs in y instead. And set the camera up-vector to z-up (default is y up).

Speed/memory for this won't matter. You are just updating a single camera once per frame, that's not something expensive.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: How can I make this in fewer lines?

Post by Mel »

Believe me, there are worse stuff out there than just writting too much :D

If you want a more unified solution, perhaps, you could check all your keys at once and update some variables regarding them, and later, construct a single direction to move the camera using that information.

If you want to move horizontally, you move on the X and Z axes. To move vertically, move on the Y axis.
Keep it up!
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Post Reply