GLBasic forum

Codesnippets => 3D-snippets => Topic started by: Kitty Hello on 2008-Jun-17

Title: 3D Entity System [EN]
Post by: Kitty Hello on 2008-Jun-17
Get the latest code here:
http://www.glbasic.com/beta/EntitySystem.zip (http://www.glbasic.com/beta/EntitySystem.zip)
Title: Re: 3D Entity System [EN]
Post by: Moru on 2008-Jul-04
So how is work progressing on this code? From what I see on the german thread there is a lot going on, is it stable yet? Should I bother look at it or are you going to change lots of stuff and only deliver german documentation?
Title: Re: 3D Entity System [EN]
Post by: Quentin on 2008-Jul-04
for me it's working great just as it is. I'm trying to create a simple Space-Shooter with. The basic movements are very simple to realize. Give it a try ;) Perhaps if you work with it, you will have new ideas what's missing.
Title: Re: 3D Entity System [EN]
Post by: Moru on 2008-Jul-05
Well, one idea would be to announce here (in english) when there is an update instead of having to download and do a file-compare to figure what was done :-)
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2008-Jul-06
Apologies! The changes are at the top of the T3D.gbas file (or whatever it's called)
Title: Re: 3D Entity System [EN]
Post by: Hemlos on 2009-Jan-25
Hello,

It did work/compile mid 2008 without problem.
I downloaded the new system and tried to compile it, the program crashes now.
Title: Re: 3D Entity System [EN]
Post by: Schranz0r on 2009-Jan-26
Work fine....
No Crash :|
Title: Re: 3D Entity System [EN]
Post by: Hemlos on 2009-Jan-26
You try it on win xp ?

I will debug the crash if you cant find the problem.
Title: Re: 3D Entity System [EN]
Post by: Moru on 2009-Feb-08
Is there any way of rendering shadows with the Entity system? Or is there some way I can get shadows anyway?

I tried EntityEnableShadow(new_obj.obj, TRUE) but this just makes all textures darker...
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2009-Feb-09
Yes, see the demo. Did you put the light source to a proper place?
Title: Re: 3D Entity System [EN]
Post by: Hemlos on 2009-Feb-09
Definetly some bug, i redownloaded, and recompiled, the demo crashes on startup. (see screenshot)

I think its an issue with the arrays....i find this is typical, it is what usually causes a program to crash.
I tried different ways of solving it, but i have not succeeded.

This is the source of the crash:

Code: Text [Select]
X_CAMERA o.Mg[12], o.Mg[13], o.Mg[14], o.Mg[12]+o.Mg[8], o.Mg[13]+o.Mg[9], o.Mg[14]+o.Mg[10]
 

If i REM this out, the program starts , but as i move the entity with the arrow keys, it crashes again.

[attachment deleted by admin]
Title: Re: 3D Entity System [EN]
Post by: Moru on 2009-Feb-09
The demo runs just fine for me as long as I don't press the [ and ] buttons.

Light is placed above and to the side, like this:
Code: GLBasic [Select]
        light = EntityCreateLight(RGB(255,255,255))
        EntityMove(light, -150, 150, 150)

also, if I activate the bumpmap with just a plain white .png the program just exits without errors when I add the second type of objects. One type is fine but the second (in whatever order) I add just gets me thrown out.

When I add a camera with this code:

Code: GLBasic [Select]
        RESTORE cameras
        FOR n = 0 TO 4
                READ cam.x, cam.y, cam.z
                READ cam.rx, cam.ry, cam.rz
                cam.obj = EntityCreateCamera()
                EntitySetCameraRange(cam.obj, 1, MAX_RANGE)
                EntitySetPosition(cam.obj, cam.x, cam.y, cam.z)
                EntitySetRotation(cam.obj, cam.rx, cam.ry, cam.rz)
                DIMPUSH camera[], cam           // Add the camera to the array of cams
                DEBUG "obj:"+camera[n].obj+"\n"
        NEXT
 
The entity ID I get back is as follows:
obj[0]:0
obj[1]:1
obj[2]:0
obj[3]:1
obj[4]:0
When I try to activate those cameras nothing happens except the skybox disappears. Is there some problem with creating the skybox as in the tutorial?
Title: Re: 3D Entity System [EN]
Post by: Quentin on 2009-Feb-09
hmm the entity id should never be 0. In this case something went wrong when creating the entity. Is it possible you post your complete project?
Title: Re: 3D Entity System [EN]
Post by: Moru on 2009-Feb-09
I have cut out everything except for this part of the project, the rest is not used:
CreateSphere() is from the samples.

Code: GLBasic [Select]
Init3d()
END


TYPE OBJECT                                     // The 3D objects
        obj                                             // Object ID
        x; y; z                                 // Position
        dx; dy; dz                              // delta for movement
        rx; ry; rz                              // rotation
ENDTYPE


FUNCTION Init3d:
        LOCAL cam AS OBJECT

        // Load textures
        gfx.brick1 = DynLoadSprite("texture/brick1.png")
        gfx.concrete1 = DynLoadSprite("texture/concrete1.png")
        gfx.concrete2 = DynLoadSprite("texture/concrete2.png")
        gfx.concrete3 = DynLoadSprite("texture/concrete3.png")
        gfx.concrete4 = DynLoadSprite("texture/concrete4.png")
        gfx.concrete_purple = DynLoadSprite("texture/concrete_purple.png")
        gfx.cracks = DynLoadSprite("texture/cracks.png")
        gfx.diagonal = DynLoadSprite("texture/diagonal.png")
        gfx.sand = DynLoadSprite("texture/sand.png")
        gfx.sunrise = DynLoadSprite("texture/sunrise2.png")
        gfx.white = DynLoadSprite("texture/white.png")

        RESTORE cameras
        FOR n = 0 TO 4
                READ cam.x, cam.y, cam.z
                READ cam.rx, cam.ry, cam.rz
                cam.obj = EntityCreateCamera()
                EntitySetCameraRange(cam.obj, 1, MAX_RANGE)
                EntitySetPosition(cam.obj, cam.x, cam.y, cam.z)
                EntitySetRotation(cam.obj, cam.rx, cam.ry, cam.rz)
                DIMPUSH camera[], cam           // Add the camera to the array of cams
                DEBUG "obj:"+camera[n].obj+"\n"
        NEXT
        active_cam = 4                                  // The last added camera is the active camera

        light = EntityCreateLight(RGB(255,255,255))
        EntityMove(light, -150, 150, 150)

        // Skybox (or ball in this case)
        CreateSphere(0, 2, 35, RGB(255,255,255))

ENDFUNCTION


//! Load sprites. Animations and sprites seems to share ID's
FUNCTION DynLoadSprite: file$
    LOCAL free_texture
    free_texture = EntityGetFreeTexture()
    LOADSPRITE file$, free_texture
    RETURN free_texture
ENDFUNCTION
 
Title: Re: 3D Entity System [EN]
Post by: Quentin on 2009-Feb-09
its hard fo find an error in this snippet. Is not running without several edits.

a few remarks:
- did you define variable MAX_RANGE ?
- your function DynLoadSprite is not needed, use EntityLoadTexture instead, it does what you want

perhaps you could post the complete code without graphics?
Title: Re: 3D Entity System [EN]
Post by: Moru on 2009-Feb-09
MAX_RANGE is from your code, is 1000.
DynLoadSprite is something I've been using long so I just keept using it. Didn't know there was anything in the entity layer that could do it, thanks for the tip :-)

Ok, this is the complete (cut off the 2D unimportant stuff and actually tested the project :-)

Code: GLBasic [Select]
// --------------------------------- //
// Project: Help-Forum
// Start: Sunday, February 08, 2009
// IDE Version: 6.156

Init3d()
END


TYPE OBJECT                                     // The 3D objects
        obj                                             // Object ID
        x; y; z                                 // Position
        dx; dy; dz                              // delta for movement
        rx; ry; rz                              // rotation
ENDTYPE


GLOBAL MAX_RANGE = 1000
GLOBAL camera[] AS OBJECT


FUNCTION Init3d:
        LOCAL cam AS OBJECT

        RESTORE cameras
        FOR n = 0 TO 4
                READ cam.x, cam.y, cam.z
                READ cam.rx, cam.ry, cam.rz
                cam.obj = EntityCreateCamera()
                EntitySetCameraRange(cam.obj, 1, MAX_RANGE)
                EntitySetPosition(cam.obj, cam.x, cam.y, cam.z)
                EntitySetRotation(cam.obj, cam.rx, cam.ry, cam.rz)
                DIMPUSH camera[], cam           // Add the camera to the array of cams
                DEBUG "obj:"+camera[n].obj+"\n"
        NEXT
        active_cam = 4                                  // The last added camera is the active camera

        light = EntityCreateLight(RGB(255,255,255))
        EntityMove(light, -150, 150, 150)

ENDFUNCTION


STARTDATA cameras:
        DATA 35, 70, 150        // Position of camera 0
        DATA 25, 180, 0         // rotation
        DATA 35, 70, -150       // Position of camera 1
        DATA 25, 0, 0           // rotation
        DATA 35, 70, 150        // Position of camera 2
        DATA 25, 180, 0         // rotation
        DATA 35, 70, 150        // Position of camera 3
        DATA 25, 180, 0         // rotation
        DATA 35, 70, 150        // Position of camera 4
        DATA 25, 180, 0         // rotation
ENDDATA
 

Edit: EntityLoadTexture() is not doing what I want it to do, it doesn't load animations... and since sprites and animations share the index I have to write my own routine to load animations anyway.
Title: Re: 3D Entity System [EN]
Post by: Quentin on 2009-Feb-09
ahh yeah, now it seems to be clear.

my fault. I thought entity id's starts by 1, but they start at 0

Regarding your cameras:
Seems to be a bug of the entity system. You can create more than one camera. BUT: If you create the second camera, the first one is disabled. (active = FALSE). Thus if you create the third camera, it will overwrite the first one, as the state of the first camera is inactive. :((

Code: GLBasic [Select]
FUNCTION EntityCreateCamera:
LOCAL t AS T3DEntity
LOCAL e
        t.what = 1
        t.name$="camera"
        e = EntityAddInternal(t)
        //EntityEnable(e, TRUE)
        RETURN e
ENDFUNCTION
 

This is a modified version of EntityCreateCamera. The command EntityEnable is commented. So you must call EntityEnable is your code to set the current camera. Now the cameras are numbered correctly from 0 to 4 in the debug window.
BUT in case you create new Entity objects after the camera creation the inactive cameras will be overwritten again. Too bad. Until Gernot has fixed this, you will have to work with only one camera. In most cases this should be sufficient. You can easily turn your camera if every direction you want. Just remember camera positions and rotation angles in your cam TYPE and use EntitySetPosition and EntitySetRotation for replacement.

Hope this will help you.
Title: Re: 3D Entity System [EN]
Post by: Moru on 2009-Feb-09
No problem, that was the way I was going to do this before I saw that you can have more than one camera. Thought I could make it easy for myself :-)
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Sep-07
New command EntityMoveGlobal2D - places an entity on the screen's pixel coordinates.
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Sep-07
Cool. So I should call this every showscreen loop to keep a 3D object at the same position on the screen? Where can I download the updated version from?

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Sep-08
Link is the same as in post #1.
Once you have set the position and don't move the camera, the object will stay there (that's the point of the ES).
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Sep-08
Sorry, should have added that my game has the camera moving around all the time. I assume then you need to keep calling that routine every time the camera moved. Would be nice though if you could set the 2D coords for a 3D object and then the ES would sort it out for you for each draw loop.

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Sep-08
Ah, that way. Interesting idea.
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Sep-08
Yeah, I was just worried about using the movement commands inside the ES drawing routine as I really didn't know what I was doing in that bit, especially about when to move the object etc.

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Sep-09
Update. Object stays at 2d pixel position.
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Sep-09
Hmm, dunno if I'm doing it wrong but it doesn't seem to stay in the same place. I setup an object like this:-
Code: GLBasic [Select]
GLOBAL testobj%,testtexture%,testtexturex%,testtexturey%
testtexture=EntityLoadTexture("home.png")
testobj=EntityCreatePlain(10,10)
EntityApplyTexture(testobj,testtexture,-1)
EntityRotate(testobj,90,0,0)
EntitySetPosition2D(testobj,10,10,-1)
And it put the object in the middle of the screen. And then when I moved around it didn't stay in the same place on the screen. I put some debug into the main drawall section and can see the values of wx and wy changing all the time but it doesn't seem to have an effect. Note my camera has moved around and rotated a load if this effects anything. Plus is there a way to say lets put a 3D object at this point on the screen at this rotation? Main reason is so that we can use 3D objects as text, HUD images etc. If an object rotates with the rest of the game then it won't work. And can you turn lighting off on a selected object with the ES system, but every other object in the game is effected by lighting? Just so can then do the HUD objects without worry about lighting effects on them.

Note my code above is outside the main game loop and nothing else is ever done with the object.

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Nov-10
New update fixed the EntitySetPosition2D command and adds an example how to use it.
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Nov-10
Damn. Stupid sunny holiday stopping me from using GLB!  Hmm nevermind will console myself with sun, beach, and sea... Rats... :)

Thanks a lot Gernot! Will check it out when I get back.

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Nov-10
Yes. Great  :glare: It's November here in Germany. The most ugly time of the year, and you talk about beaches...
First thing I do when I win in a lottery is move to Florida.
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Nov-11
Heh it's November in Cyprus too. Just it's very nice weather :) Apparently we may get some rain on Wednesday. Otherwise around 24c and sunny :)

Cheers
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Dec-18
Gernot, is the attached zip at on the first post including the updates mentioned above? It's just that you mention it's updated in November but the modification date of the first post is in September. Plus integrating the first post code into my game doesn't seem to work for me (can't even see the object).

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Dec-19
Yes, it should be all in. Did an older version work and now it fails!?
Title: Re: 3D Entity System [EN]
Post by: Crivens on 2011-Dec-19
 No the fixed 2d positioning has never worked in my game.

Also the 3DES on its own doesn't seem to compile for me.

Cheers
Title: Re: 3D Entity System [EN]
Post by: Kitty Hello on 2011-Dec-20
Download again. It should say version 0.8.4.
Also the example I attached (when running - the ball "game") uses the 2D position to place a small sphere in the top left corner.
Title: Re: 3D Entity System [EN]
Post by: ProN3rd on 2015-Nov-09
hey i was wondering about the practical implications of such an entity component system based approach for smaller projects. I sure see the advantages for enormous projects that involve many people and years of development. But what about a small indie game that only consists of a few thousand lines? There is so much exception handling going on, i couldnt even image how to realize this effecintly with an entity approach.
Title: Re: 3D Entity System [EN]
Post by: bigsofty on 2016-Apr-20
I had a question about a sub in the code but It was one of those "Doh, I shoulda seen that!", right after this post... and there's no delete post option I see. :D