Drawing 2D and having a Camera for movement?

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.
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Drawing 2D and having a Camera for movement?

Post by Zerowalker »

Okay i have come as far as i am able to draw stuff more or less, and play around with collision etc.

However i am limited to the resolution as i have no idea how i can make a platformer so i can move and the world moves with the player.
I tried playing around with cameras in irrlicht, but it only affects 3D, so i maybe i have to somehow draw in the 3D world or something?

Currently can set out blocks in 32x32 and save them in a "map" file with it's location (X,Y) and ID (Texture).

So would someone like to point me in the right direction?
I am totally clueless so bare with me;P

Thanks!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

If you want to avoid 3d completely you'd have to draw things with an offset (player walks left means world moves to right - so you draw everything with +x pixels).

But often it's better to use some 3D. You draw your world into a texture and put that on a plane. For a start you can use for example a billboard scenenode. Thought I would use a real plane probably. For example model 2 triangles in some tool like Blender and load it as mesh (later you can create it directly in code). Then you can do more things like rotation which billboards don't allow. Experiment maybe with an orthographic camera it could be easier for some things.

Advantage of working in 3D is that it is easier to add some effects later to your world. You can zoom, rotated the camera, rotated the world, etc. Also it might help to put all your objects in the world each in their own node. You can draw them on top of the world by drawing them later and ignoring the zbuffer while drawing (zbuffer is a setting in SMaterial, while drawing later means you don't draw with scenemanger::drawAll but with call render() for the nodes directly in the order you want).

I hope that gave you some ideas.
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

Ah, well i kinda get what you mean with that 3d thing, and i actually was trying to do something like that as i got that it would be the easy way instead of doing some weird offset thing.

But i don't get what you mean with making the model in blender, can i just make a plane there and then scale it in irrlicht at will?
Or do i have to somehow calculate everything, as you know different maps might have different width etc?

Never really used much of 3D in irrlicht, i was able to do the render to texture and have that cube, so i am guessing it's something like that?

If it's easiest to use the billboard or something i might go with that as a start. I am a beginner and just getting things to work at all is a huge step, then i try to slowly add functions and improvements and learn:)
Thanks!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

Billboard is probably the easiest for a start. It just shows a texture which faces the camera. But doesn't allow rotating. The idea with creating a plane in blender and loading that mesh was just because I thought Irrlicht had no way to create a SceneNode with a plane. But realizing now there is a simple way:
1. Create a mesh IGeometryCreator::createPlaneMesh (you get the IGeometryCreator from ISceneManager).
2. Create a meshscenenode with that mesh (ISceneManager has a function for that).

Those planes can even have tiles which might be useful maybe (otherwise just set that number to 1).
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

How can i make the mesh/plane face the camera?
I am having much trouble coordinating it, billboard was a bit easier but i still have troubles with positioning etc:S
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

Think of it like a real camera that you try to point at a piece of paper. You have 2 choices - point the camera at the paper or move the paper until it's in front of the camera. Same in 3D. A camera has a position, a target and an up-vector. Position is where camera is - target is where it looks at - and up-vector is the direction is the direction which should be up (if you want a camera to stand on the head while looking at the target the up-vector would point down).

Might help if you start with a camera you can move around like maya-camera or fps-camera. Just put your mesh somewhere and print out those 3 vectors (position, target, up) while moving the camera around it manually. Then you start to get a feeling for it.
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

Yeah i can kinda do it, i get the concept.

But isn't there like some kind of way to figure how to get the distance between the camera and the mesh to be 1x1.
Like, i have the mesh at a distance provides equal "pixel-density" as drawing on the screen would.

I have done this manually, but it took forever and the position doesn't make much sense, and it breaks if i change resolution:S
There must be some kind of formula for this, but i just can't figure it out.


EDIT:

Here is how i currently have it:

billboard->setPosition(core::vector3df((312+640) - PX, (-52 - 480) + PY, 331/zoom));
(PX,PY = Player Position), so as yo ucan see, "312, -52 and 331" doesn't make any sense, it just works with those,
but it doesn't scale if resolution changes (resolution is 640x480 as you you can see, i thought it would scale if i changed but it doesn't).
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Drawing 2D and having a Camera for movement?

Post by hendu »

Such "equal density" is called a screen quad. Search the code snippets.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

Or look into XEffects, to my knowledge it has one.
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

screen quad, effects?

Wait, wait.

I just mean.

If i create a billboard, render a texture on it.
How do i know what distance it must have from the camera in order to be 1x1 in pixels, meaning it's not zoomed in/out, it looks exactly like if i was painting directly on the screen.
Cause currently, it seems to be "331", which doesn't tell me anything, i have no idea why it's that number, i just hade to try manually until i noticed the pixels were 1x1.

And i am movign the billboard, no the camera. I have a fiexed Camera with the default position and settings.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

Go there: https://bitbucket.org/mzeilfelder/irr-playground-micha
Go into sources and look for infinityZoom.cpp
There's a function called getScreenFillDistance which basically does that calculation.
You will also need the updateCameraFrustum() as it calculates one of the parameters needed in there.

At least I think it did that... been a while since I wrote it.
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

I tried it, but the only thing it seems to do is make it fit into the camera, that's not my goal.
My goal is to make it appear 1x1, no stretching, the entire billboard doesn't need to be visible it just needs to be at the distance so the texture rendered at it appears as if it was rendered on the screen.:)
Last edited by Zerowalker on Mon Aug 10, 2015 2:40 pm, edited 3 times in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Drawing 2D and having a Camera for movement?

Post by CuteAlien »

Not exactly - it fits a billboard with the *given dimensions* to the screen. Once your texture-size is not the same as the billboard-size the texture is stretched again. Also when the billboard doesn't have the same size as the screen. But that should then be simple scaling factors by some constant. Say your billboard is twice the texture-size that means your texture-pixels are strechted by factor 2. So you have to pass half the dimensions to get them unscaled. There's still some math involved there... but you can make it easier if you ensure that your billboards have the same width/height ratio than the screen (don't have to be the same - but same ratio makes it a little easier as you only have to scale by a single value then).

So start with my function - then do a little scaling. I'm sorry - I don't have the time to write that right now.
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
Zerowalker
Posts: 20
Joined: Mon Feb 16, 2015 3:05 pm

Re: Drawing 2D and having a Camera for movement?

Post by Zerowalker »

I think i get what you are saying.
Problem is i use billboard as a stage, a "2D map", so it won't be following the aspect ratio, as it depends on the map i want.
Often the width is probably much greater than the height (as the Player will probably be spawning on the left side and going towards the right).

Of course i could make it follow the aspect ratio, but won't it be so much "dead space" then, not sure if this has impact on performance or not though?

I am not expecting you to write the code for me or anything so don't worry about that:)

Thanks:)


EDIT:

Here is a pic:

Image

The blocks are 16x16, and in that image it's on a billboard, however the distance is incorrect as you can see that the blocks are distorted.
I need to be able to figure out (no matter the billboard resolution), the distance in order for those 16x16 (or whatever) blocks to appear as 16x16 on the screen, they should look identical to it's original form so to speak.
This is all probably very easy and it's just me not being able to understand it all.

I can move Camera or billboard, or even use something else as a plane/mesh. Anything will do as long as i can have it on the screen as a image that moves in order to "follow the player" so to speak.
As i am guessing this is the way to do that stuff from previous posts.

The White Block is NOT 16x16, it can be ignored in this pic, not that it really matters.

Thanks:)
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Drawing 2D and having a Camera for movement?

Post by mongoose7 »

Use an orthographic camera.
Post Reply