Results 1 to 3 of 3

Thread: Help structuring Entity System

  1. #1

    Help structuring Entity System

    Hi all,

    Recently I've got into the world of 3D, and I've been playing around a lot with it. Currently I'm trying to make an entity system(aka object factory or pluggable factory).

    I'm trying to mimick BlitzBasic3D's system, as I really like the cleanliness of how it's done.

    Blitz 3D Manual

    Here's the class I wrote that can handle rotation,scaling,and position of objects. There's a few things I'd like to add, though I'm not sure how to properly implement them.

    [pascal]unit objecthandle;

    interface

    // External Dependencies
    uses
    SysUtils,
    OpenGL,
    glfw;

    procedure tri(size:single);
    // [ ===== Entity =====]
    // The entity class which handles object translation,rotation, and scaling.
    type
    entity = class
    xpos,ypos,zpos:single;
    angle,xrot,yrot,zrot:single;
    xscale,yscale,zscale:single;
    procedure position(xpos,ypos,zpos:single);
    procedure rotation(angle,xrot,yrot,zrot:single);
    procedure scale(xscale,yscale,zscale:single);

    end;
    // [ ===== Entity =====]


    implementation


    // [ = Position the entity = ]
    procedure entity.position(xpos,ypos,zpos:single);
    begin
    gltranslatef( xpos,ypos,zpos);
    end;

    // [ = Rotate the entity = ]
    procedure entity.rotation(angle,xrot,yrot,zrot:single);
    begin
    glrotatef(angle, xrot,yrot,zrot);
    end;

    // [ = Scale the entity = ]
    procedure entity.scale(xscale,yscale,zscale:single);
    begin
    glscalef(xscale,yscale,zscale);
    end;

    procedure tri(size:single);
    begin
    glBegin( GL_TRIANGLES );
    glColor3f( 1.0, 0.0, 0.0 );
    glVertex3f( -5.0-size, 0.0, -4.0-size );
    glColor3f( 0.0, 1.0, 0.0 );
    glVertex3f( 5.0+size, 0.0, -4.0-size );
    glColor3f( 0.0, 0.0, 1.0 );
    glVertex3f( 0.0, 0.0, 6.0+size);
    glEnd;
    end;





    end.[/pascal]

    Here's a little diagram I drew to hopefully make my intentions more clear.

    [pascal][/pascal]

    There are a few problems which I can't get my head around. The first being, how do I define what my entity is? I suppose I could put something in my procedures for when I create my objects that automatically assigns what entity they are, and there unique id(Hmm, might've actually solved my own problem while writting this post :shock: ).

    The second problem though, is how can I prevent lights from being scaled. Since everything is manipulated from a common class, what stops a user from scaling a light? Would it just be easier to write a new class for the lighting functionality?


    Well, I know I've kind of rambled a bit, but I'd like to know how you structure your games and manage all the different types of objects. Am I heading in the right or wrong direction?

    Thanks for any help.


  2. #2

    Help structuring Entity System

    Here is a way to implement the entity and light with scaling disabled for the light.
    [pascal]type
    TVector3 = record
    x, y, z: Single;
    end;

    TEntity = class
    protected
    FPos: TVector3;
    FRotAxis: TVector3;
    FScale: TVector3;
    FAngle: Single;
    FID: Integer;
    protected
    procedure SetPos(const p: TVector3); virtual;
    procedure SetRotAxis(const a: TVector3); virtual;
    procedure SetAngle(a: Single); virtual;
    procedure SetScale(const s: TVector3); virtual;
    public
    constructor Create(AID: Integer); virtual;
    procedure Apply; virtual;
    property Pos: TVector3 read FPos write SetPos;
    property RotAxis: TVector3 read FRotAxis write SetRotAxis;
    property Angle: Single read FAngle write SetAngle;
    property Scale: TVector3 read FScale write SetScale;
    property ID: Integer read FID;
    end;

    TLight = class(TEntity)
    protected
    procedure SetScale(const s: TVector3); override;
    public
    procedure Apply; override;
    end;

    function Vector3(x, y, z: Single): TVector3;

    implementation

    function Vector3(x, y, z: Single): TVector3;
    begin
    Result.x := x;
    Result.y := y;
    Result.z := z;
    end;

    { TEntity }

    constructor TEntity.Create(AID: Integer);
    begin
    FID := AID;
    FPos := Vector3(0.0, 0.0, 0.0);
    FRotAxis := Vector3(1.0, 0.0, 0.0);
    FScale := Vector3(1.0, 1.0, 1.0);
    FAngle := 0.0;
    end;

    procedure TEntity.Apply;
    begin
    glRotatef(FAngle, FRotAxis.x, FRotAxis.y, FRotAxis.z);
    glScalef(FScale.x, FScale.y, FScale.z);
    glTranslatef(Fpos.x, FPos.y, FPos.z);
    end;

    procedure TEntity.SetPos(const p: TVector3);
    begin
    FPos := p;
    end;

    procedure TEntity.SetRotAxis(const a: TVector3);
    begin
    FRotAxis := a;
    end;

    procedure TEntity.SetAngle(a: Single);
    begin
    FAngle := angle;
    end;

    procedure TEntity.SetScale(const s: TVector3);
    begin
    FScale := s;
    end;

    { TLight }

    procedure TLight.SetScale(const s: TVector3);
    begin
    { Do nothing. Do not call inherited. }
    end;

    procedure TLight.Apply;
    begin
    { Call the inherited Apply to set the pos, rot, etc. }
    inherited;
    { Set the light's properties here, ie. colour, range, etc. }
    { ... }
    end;[/pascal]

    I hope that helps.

  3. #3

    Help structuring Entity System

    Awesome, I see exactly how I can prevent lights from scaling.

    I'm still having problems with creating unique objects, but that is pretty trivial. I've got my method of organizing it, just need to get it to compile without erros.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •