About Store Forum Documentation Contact
Donations:
309$/mo



Post Reply 
Creating Objects Programmatically
Author Message
andargor Offline
Member

Post: #1
Creating Objects Programmatically
Is it possible to create objects purely programmatically? I can already dynamically create objects (from the tutorial), but I have to load an object that has a predefined mesh to do so.

I would like to create shapes, boxes, spheres, perhaps assemble them, apply textures, create physics, and create an object out of that. Not static, probably derived from Game::Item and it would move around.

Doable? if so, how? Can Game::World.objCreate be used?
04-25-2011 06:48 AM
Find all posts by this user Quote this message in a reply
Rabishan Offline
Member

Post: #2
RE: Creating Objects Programmatically
do you have rpg 2 source code.
if so then the player created in that game is by program.
i don't know how to explain you exactly.
but if you see NewGameClass (Menu.cpp) and InitGame(Game.cpp) you can get some idea.
04-25-2011 08:25 AM
Find all posts by this user Quote this message in a reply
andargor Offline
Member

Post: #3
RE: Creating Objects Programmatically
Unfortunately I need the license for RPG 2 source, so not yet. smile

I guess a code snippet would be out of the question, but if you could tell me the steps, I could probably figure things out.

Otherwise I'll read all the headers for hints.
(This post was last modified: 04-25-2011 04:48 PM by andargor.)
04-25-2011 04:48 PM
Find all posts by this user Quote this message in a reply
Truelegend Offline
Member

Post: #4
RE: Creating Objects Programmatically
I dont know you mean this but
Code:
Object MyGameObject;
Actor   MyActor;
...?
04-25-2011 05:11 PM
Find all posts by this user Quote this message in a reply
andargor Offline
Member

Post: #5
RE: Creating Objects Programmatically
What I mean is creating objects purely by programming, and not loading files. This is as close as I get for now, but nothing appears (object volume reads as 0, it doesn't seem to have any size, so I'm looking into that).

EDIT: Nothing visible appears, but the object does get created and put into the game world.

Also, I'm worried about pointer safety and memory cleanup. Does Esenthel clean these up on exit? Or I have to use something like the Reference class?

Code:
        MeshBase mb;
        mb.create(Box(0.3f));

        MaterialPtr mat = new Material();
        mat->color = Vec4(0.5f,0.5f,0.5f,0.5f);

        MeshPtr mesh = new Mesh();
        mesh->add(mb);
        mesh->setRender(); // EDIT: this makes the object visible

        Game::ObjParams obj;
        obj.type(true, "OBJ_VEHICLE");
        obj.mesh(true, mesh);
        obj.material(true, mat);

        Game::Obj *g = Game::World.objCreateNear(obj,Matrix(obj.scale(),Vec(0,0,0)));

Ok, adding mesh->setRender() after the add makes it appear.

The question about the pointers and memory still stands. And why does it report 0 volume/0 size?
(This post was last modified: 04-25-2011 09:28 PM by andargor.)
04-25-2011 09:13 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #6
RE: Creating Objects Programmatically
Hi,

I made a small example creating a sphere from code + physics. Dunno if this is what you are looking for but its basically a good base to start from smile

Yes everything will be cleaned which is EE related.

PHP Code:
/******************************************************************************/
#include "stdafx.h"
/******************************************************************************/
Actor groundballActor;

Mesh ballMesh;
PhysBody ballphys;

Flt size;
/******************************************************************************/
void InitPre()
{
    
App.name("Simple Code");
    
App.flag=APP_MS_EXCLUSIVE;
    
Paks.add("../data/engine.pak");
    
D.sync(true);
}
Bool Init()
{
    
Cam.dist=4;
    
size 0.5f;

    
Physics.create(CSS_NONE,true,"../Installation/PhysX");
    
ground .create(Box (15,1,15,Vec(0,-5,0)), 0);

    
ballMesh.parts.New().base.create(Ball(size),VTX_TEX0|VTX_NRM|VTX_TNG);
    
ballMesh.setMaterial(Materials.ptrRequire("../data/mtrl/brick/0.mtrl")).setRender().setBox();    

    
ballActor  .create(Ball(size   ,Vec(0.0f00)))
        .
adamping(5   )
        .
ccd     (true);

    return 
true;
}
/******************************************************************************/
void Shut()
{
}
/******************************************************************************/
Bool Update()
{
    if(
Kb.bp(KB_ESC))return false;
    
CamHandle(0.1f,10,CAMH_ZOOM|CAMH_ROT);

    
Physics.startSimulation().stopSimulation();

    if(
Kb.bp(KB_SPACE))ballActor.addVel(Vec(0,3,0));

    return 
true;
}
void Render()
{
    switch(
Renderer())
    {
    case 
RM_PREPARE:
        {
            
ballMeshdraw(ballActor.matrix(), ballActor.vel(),ballActor.angVel());

            
LightPoint(20,Vec(0,3,-3)).add();
        }break;
    }
}
void Draw()
{
    
Renderer(Render);

    
ground.draw();


There is always evil somewhere, you just have to look for it properly.
(This post was last modified: 04-25-2011 09:36 PM by Dynad.)
04-25-2011 09:35 PM
Visit this user's website Find all posts by this user Quote this message in a reply
andargor Offline
Member

Post: #7
RE: Creating Objects Programmatically
Cool, thanks! I see that your example doesn't use Game::World. Is that faster for a large number of objects/actors?

Also ballphys doesn't seem to be used. I used your shortcut for mesh creation, however, makes things a bit tidier. I'm trying to add physics atm, with something like this:

PhysBodyPtr pb = new PhysBody();
pb->parts.New().createMesh(*mesh);

Not working yet... grin
04-25-2011 10:33 PM
Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #8
RE: Creating Objects Programmatically
createMesh works only on static objects.

There is always evil somewhere, you just have to look for it properly.
04-25-2011 10:37 PM
Visit this user's website Find all posts by this user Quote this message in a reply
andargor Offline
Member

Post: #9
RE: Creating Objects Programmatically
yay! got it. Now I only have to figure out how to create materials.

EDIT: Got the material too, I've updated the code below

PHP Code:
        MaterialPtr mat = new Material();
        
mat->color Vec4(0.2f,0.2f,0.7f,0.7f);
        
mat->ambient Vec(0.2f,0.2f,0.2f);
        
mat->specular 0.5f;
        
mat->validate();

        
MeshPtr mesh = new Mesh();
        
mesh->parts.New().base.create(Box(0.3f),VTX_TEX0|VTX_NRM|VTX_TNG);
        
//mesh->setMaterial(Materials.ptrRequire("data/mtrl/brick/0.mtrl")).setRender().setBox();
        
mesh->setMaterial(mat).setRender().setBox();

        
PhysBodyPtr pb = new PhysBody();
        
pb->parts.New().create(mesh->box);

        
Game::ObjParams obj;
        
obj.type(true"OBJ_VEHICLE");
        
obj.mesh(truemesh);
        
obj.phys(truepb);

        
Game::Obj *Game::World.objCreateNear(obj,Matrix(obj.scale(),Vec(0,0,0))); 
(This post was last modified: 04-25-2011 11:11 PM by andargor.)
04-25-2011 10:41 PM
Find all posts by this user Quote this message in a reply
Post Reply