About Store Forum Documentation Contact



Post Reply 
Scale Custom Object
Author Message
fatcoder Offline
Member

Post: #1
Scale Custom Object
In the World Editor I can scale objects and when I load them into my game as a Static object or a Chr, they have the same scale as in the World Editor. But when I try to load them in as my own custom class, they no longer have the same scale. They are loaded at the original mesh size.

In the create function of my custom object I use the following code.

matrix = obj.matrixFinal();

This positions, the object correctly, but doesn't scale it. I notice that ObjParams has a scale property, but it is always 1.0 for some reason. I know my object is scaled, because if I switch my code to load it as a Static object, it is loaded at the correct scale that I set in the World Editor. What am I missing?
07-18-2010 10:13 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #2
RE: Scale Custom Object
what are the values of the 'matrix' after =obj.matrixfinal?
07-19-2010 12:09 PM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #3
RE: Scale Custom Object
Here is the matrix that is passed in via the Game::ObjParams in the object's create() function. Notice there is no scaling at all in this matrix. It is just the identity matrix with a position.

x {x=1.00000000 y=0.00000000 z=0.00000000}
y {x=0.00000000 y=1.00000000 z=0.00000000}
z {x=0.00000000 y=0.00000000 z=1.00000000}
pos {x=-6.3188930 y=1.79659640 z=73.1132130}

And here is how I load an object into the game world from its obj file. In this example, I'm using the Human object that comes with Esenthel. If I load the Human object into the World Editor, it has a scale of 1.7.

Game::World.objCreateNear(*Game::Objs("Obj/chr/Human/0.obj"), p);
I also just modified the Extending Game Object Class tutorial to dynamically load a Human object, and it has the same problem.
(This post was last modified: 07-20-2010 01:35 AM by fatcoder.)
07-20-2010 01:21 AM
Find all posts by this user Quote this message in a reply
Esenthel Online
Administrator

Post: #4
RE: Scale Custom Object
could you describe step by step what do you do?
obj_params.matrixFinal should work fine,
make sure while debugging you actually test the scaled object, not some other instance of an object with no scale.
07-20-2010 12:06 PM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #5
RE: Scale Custom Object
OK, here is the modified code from the Extending Game Object Class tutorial. I have modified it to load the full matrix, instead of just the position. I have also changed it to load the Human chr when the spacebar is pressed, instead of a barrel. You should be able to copy and paste this all in and just hit run.

Step into the create function for the NewObj and take a look at _matrix after it is set. There is no scale at all. Then you can add _matrix.scale(1.7f); after the matrix is set to test.

Code:
/******************************************************************************/
#include "stdafx.h"
#include "../../../../../data/enum/_enums.h"
/******************************************************************************

   In this tutorial is presented how to extend a Game Object class.
   But unlike in previous examples, we won't base on Character/Item or other classes.
   This time we'll extend the most base class - Game::Obj.

/******************************************************************************/
struct NewObj : Game::Obj // extend Game Object
{
   Matrix   _matrix;
   Mesh *mesh    ; // and mesh

   // provide necessary methods required by Game::Obj :
      virtual void create(Game::ObjParams &obj); // extend default creation

      virtual Vec  pos(          ) {return _matrix.pos    ;} // get position
      virtual void pos(C Vec &pos) {       _matrix.pos=pos;} // set position

      virtual Matrix matrix(                ) {return _matrix       ;} // get matrix
      virtual void   matrix(C Matrix &matrix) {       _matrix=matrix;} // set matrix

      virtual Bool update     () {return true;} // object update
      virtual UInt drawPrepare();               // object draw prepare
      virtual void drawShadow ();               // object draw shadow

   // constructor
   NewObj();

   // io methods
   virtual void save(File &f);
   virtual Bool load(File &f);
};
/******************************************************************************/
NewObj::NewObj() // initialize values in constructor
{
   _matrix = MatrixIdentity;
}
void NewObj::create(Game::ObjParams &obj)
{
   // now setup custom parameters from 'obj'
   _matrix =obj.matrixFinal(); // obtain our 'position' member from 'obj'
   mesh    =obj.mesh();            // set 'mesh'
}
/******************************************************************************/
UInt NewObj::drawPrepare()
{
   if(mesh && Frustum(mesh->box,matrix()))mesh->draw(matrix());
   return 0; // return 0 because no additional rendering modes are required
}
void NewObj::drawShadow()
{
   if(mesh && Frustum(mesh->box,matrix()))mesh->drawShadow(matrix());
}
/******************************************************************************/
void NewObj::save(File &f)
{
   __super::save(f); // default save

   f<<_matrix;           // save custom parameters
   f.putStr(Meshs(mesh)); // save mesh path
}
Bool NewObj::load(File &f)
{
   if(__super::load(f)) // if default load was successful
   {
      f>>_matrix;            // load custom parameters
      mesh=Meshs(f.getStr()); // load mesh
      return true; // return success
   }
   return false; // return failure
}
/******************************************************************************/
// MAIN
/******************************************************************************/
Game::ObjMemx<NewObj> NewObjs; // container for objects
/******************************************************************************/
void InitPre()
{
   App.name("Game::Obj class");
   App.flag=APP_FULL_TOGGLE;
   IOPath("../data");
   Paks.add("engine.pak");

   D.full(false).sync(true).ambPower(0.3).hpRt(true).viewRange(50);

   Cam.at.set(16,0,16);
   Cam.dist = 10;
   Cam.pitch=-PI_3;
}
/******************************************************************************/
Bool Init()
{
   Physics.create();
   Sky    .atmospheric();
   Sun.image=Images("gfx/sky/sun.gfx"); Sun.light_color=1-D.ambColor();

   Game::World.init      (                )
              .setObjType(NewObjs,OBJ_CHR) // use OBJ_ITEM type because objects in the world we're using are saved with OBJ_ITEM type
              .New       ("world/custom params.world");

   return true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Update()
{
   if(Kb.bp(KB_ESC))return false;

   if(Kb.bp(KB_SPACE))
      Game::World.objCreateNear( *Game::Objs( "Obj/chr/Human/0.obj" ), Vec( 0, 0, 0 ) );

   CamHandle(0.1,100,CAMH_ZOOM|(Ms.b(1)?CAMH_MOVE:CAMH_ROT));

   Game::World.update(Cam.at);

   return true;
}
/******************************************************************************/
void Render()
{
   Game::World.draw();
}
void Draw()
{
   Renderer(Render);
}
/******************************************************************************/
07-20-2010 11:10 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #6
RE: Scale Custom Object
Shouldn't this line:
Code:
Game::World.objCreateNear( *Game::Objs( "Obj/chr/Human/0.obj" ), Vec( 0, 0, 0 ) );

be this?
Code:
Game::ObjParams &obj=*Game::Objs("Obj/chr/Human/0.obj");
Game::World.objCreateNear(obj,Matrix(obj.scale(),Vec(0,0,0)));
07-21-2010 05:23 AM
Find all posts by this user Quote this message in a reply
fatcoder Offline
Member

Post: #7
RE: Scale Custom Object
Of course! It is a Matrix not Vector!!!

You have got to be kidding me! I feel like a fool... Sorry for wasting everyones time smile
07-21-2010 09:01 AM
Find all posts by this user Quote this message in a reply
Post Reply