About Store Forum Documentation Contact



Post Reply 
Moving Items
Author Message
atarax88 Offline
Member

Post: #1
Moving Items
Hi everyone,

Im new to esenthel and have a question.

I am trying to make a vehicle do some simple movement when i press down on the arrow keys.

For now I am extending from the Item base class but plan to extend from the Obj class once i have more knowledge using the engine/physics.

I have a vehicle mesh in the world editor and I have generated the Physbody in the mesheditor.

When i load the world in I put the vehicle into its own memory container and am using this code for it.

Code:
STRUCT(Vehicle , Game::Item)//extend item structure by defining a vehicle class based on an item
//{

    
virtual Bool update ();//update the vehicle
};

Bool Vehicle::update()
{
    Flt s=3;
    
    // add forces to vehicle
    if(Kb.b(KB_UP   ))T.actor.addForce(Vec(0,0, 1)*s);
    if(Kb.b(KB_DOWN ))T.actor.addForce(Vec(0,0,-1)*s);
    if(Kb.b(KB_LEFT ))T.actor.addForce(Vec(-1,0,0)*s);
    if(Kb.b(KB_RIGHT))T.actor.addForce(Vec( 1,0,0)*s);

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

return __super::update();
}

When i press the arrow keys the vehicle stays in the same place and nothing happens.
Would appreciate any help!

Also how do i log what is going on to make sure i am entering the correct functions etc? Can esenthel log to a text file?[/code]
09-25-2010 01:10 AM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #2
RE: Moving Items
Did you look at this tutorial? "EsenthelEngineSDK\Tutorials\Source\Advanced\2 - Animation, Physics, Rendering\Physics\13 - Vehicle.cpp"

(09-25-2010 01:10 AM)atarax88 Wrote:  Also how do i log what is going on to make sure i am entering the correct functions etc? Can esenthel log to a text file?[/code]

LogN(S+variable);
...or draw them to screen using D.text();
09-25-2010 01:49 AM
Find all posts by this user Quote this message in a reply
atarax88 Offline
Member

Post: #3
RE: Moving Items
yes i looked at the vehicle tutorial.

The problem is it doesn't demonstrate how you can attach that actor to a physical mesh.

The tutorial creates a physics plane and car by using actor objects in the physics world but does not go beyond that.

The Item class has an instance of an actor object which is what i am trying to use.
09-25-2010 04:35 AM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #4
RE: Moving Items
Add these methods to your Vehicle struct and the mesh should drawn where the actor is. Just make sure to load in a mesh first.

Code:
UInt drawPrepare()
{
    UInt modes = __super::drawPrepare();
    mesh->draw(matrixScaled());

    return modes;
}

void drawShadow()
{
    mesh->drawShadow(matrixScaled());
}
(This post was last modified: 09-25-2010 07:51 AM by Driklyn.)
09-25-2010 07:50 AM
Find all posts by this user Quote this message in a reply
atarax88 Offline
Member

Post: #5
RE: Moving Items
Hey thanks that worked.

Here is the code i am using to just get force added to a vehicle with the arrow keys (not good for driving around).

I also attached a screenshot of my vehicle setup.

If anyone has a better way of doing this or a custom vehicle class they would not mind sharing let me know smile




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

/******************************************************************************/

/******************************************************************************/
STRUCT(Vehicle , Game::Item)//extend item structure by defining a vehicle class based on an item
//{

virtual Bool update ();//update the vehicle
virtual UInt drawPrepare();
virtual void drawShadow();

};

Bool Vehicle::update()
{
    Flt s=3;
    
    // add forces to vehicle
    if(Kb.b(KB_UP   ))T.actor.addVel(Vec(0,0, 1)*s);
    if(Kb.b(KB_DOWN ))T.actor.addVel(Vec(0,0,-1)*s);
    if(Kb.b(KB_LEFT ))T.actor.addVel(Vec(-1,0,0)*s);
    if(Kb.b(KB_RIGHT))T.actor.addVel(Vec( 1,0,0)*s);

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

    //
    LogN("called vehicle update");

return __super::update();
}


UInt Vehicle::drawPrepare()
{
    UInt modes = __super::drawPrepare();
    mesh->draw(matrixScaled());

    return modes;
}

void Vehicle::drawShadow()
{
    mesh->drawShadow(matrixScaled());
}



Game::ObjMemx<Game::Static> Statics; // container for static objects
Game::ObjMemx<Game::Item  > Items  ; // container for item   objects
Game::ObjMemx<      Vehicle> Vehicles; //container object for vehicles






Bool InitGame()
{

    Text_ds.scale*=0.8;

    Physics.create();

    Sun.image=Images("gfx/sky/sun.gfx");
    Sky.atmospheric();

    // create the world
    Game::World.init()
        .setObjType(Statics,OBJ_STATIC)
        .setObjType(Vehicles,OBJ_VEHICLE)
        .setObjItem(Items  ,OBJ_ITEM  )// please note that here is called 'setObjItem' instead of 'setObjType', this is used for enabling built-in character<->item relations such as picking up and dropping items
        .New("world/sample.world"  );

    Cam.setSpherical(Vec(10,20,20),-PI_4,-0.5,0,30).set(); // set initial camera


   return true;
}
void ShutGame()
{



}
/******************************************************************************/
Bool UpdateGame()
{
  
    {
        Physics.startSimulation(); // start frame simulation

        // physics simulation takes some time, so you can do some  calculations here
    

        Physics.stopSimulation(); // get results of frame simulation
    }

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

   return true;
}

void Render()
{

    Game::World.draw();

}

void DrawGame()
{

    D      .clear();
    Physics.draw ();



    
    Renderer(Render);
    
}




/******************************************************************************/
State StateGame(UpdateGame,DrawGame,InitGame,ShutGame);
/******************************************************************************/


Attached File(s) Image(s)
   
09-25-2010 05:07 PM
Find all posts by this user Quote this message in a reply
atarax88 Offline
Member

Post: #6
RE: Moving Items
Now that i have the car being able to be moved can you suggest how i go about adding realistic movements with wheels.

I have a basic box around the car for physics and am wondering how i can add wheels to that box.

In the physics tutorial it uses

Code:
// create car wheels
    Wheel::Param wp; // wheel parameters
    wheel[0].create(car,Matrix().setPos(Vec( 1,-0.5, 1.5)),wp); // left -front
    wheel[1].create(car,Matrix().setPos(Vec(-1,-0.5, 1.5)),wp); // right-front
    wheel[2].create(car,Matrix().setPos(Vec( 1,-0.5,-1.5)),wp); // left -rear
    wheel[3].create(car,Matrix().setPos(Vec(-1,-0.5,-1.5)),wp); // right-rear

It attaches the wheels to a physics box called "car".


Pretty new with the 3d physics and would appreciate any help.


My Struct is now

Code:
STRUCT(Vehicle , Game::Item)//extend item structure by defining a vehicle class based on an item
//{

virtual Bool update ();//update the vehicle
virtual UInt drawPrepare();
virtual void drawShadow();
Vehicle(); //constructor

Wheel::Param wp; // wheel parameters
Wheel wheel[4]; // wheels
Flt   angle   ; // car wheel angle


};

and the vehicle constructor creates the wheels to be attached to the actor

Code:
Vehicle::Vehicle(){
    



    //create car wheels.
    wheel[0].createL(T.actor,Matrix().setPos(Vec( 1,-0.5, 1.5)),wp); // left -front
    wheel[1].createL(T.actor,Matrix().setPos(Vec(-1,-0.5, 1.5)),wp); // right-front
    wheel[2].createL(T.actor,Matrix().setPos(Vec( 1,-0.5,-1.5)),wp); // left -rear
    wheel[3].createL(T.actor,Matrix().setPos(Vec(-1,-0.5,-1.5)),wp); // right-rear


    T.actor.massCenterL(T.actor.massCenterL()-Vec(0,0.8,0)); // lower mass center

     LogN("called vehicle constructor");

__super::Item();
}

When i am just viewing the physics world the wheels are not visible anywhere.
(This post was last modified: 09-25-2010 06:54 PM by atarax88.)
09-25-2010 06:33 PM
Find all posts by this user Quote this message in a reply
Harry Offline
Member

Post: #7
RE: Moving Items
Do you update your wheels?
09-25-2010 08:52 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #8
RE: Moving Items
Use create() function instead of constructor.

Code:
void create(Game::ObjParams &obj)
{
    __super::create(obj);

    // wheel[0].create...
}
09-25-2010 09:34 PM
Find all posts by this user Quote this message in a reply
atarax88 Offline
Member

Post: #9
RE: Moving Items
hey Driklyn that worked. Thanks again!

Would you mind just letting me know why it would work in the create function but not the constructor?
also..

The wheels are the wrong direction when they are created, I tried rotating them 90 degrees by their matrix like so.
Code:
wheel[0].matrix().rotateX(90.0f);

This does not change the wheel, maybe i need to update its matrix after i set its rotation somewhere?

Thanks again, these forums are very helpful.
(This post was last modified: 09-26-2010 05:32 PM by atarax88.)
09-26-2010 04:43 PM
Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #10
RE: Moving Items
Create is when your vehicle is actually "created" in the world and ready to go, and, as such, T.actor won't be valid until after you call __super::create(obj);

And for the wheels, do this instead...

Code:
wheel[0].createL(T.actor,Matrix(Vec( 1,-0.5, 1.5)).rotateY(PI_2),wp); // left -front

Matrix(Vec( 1,-0.5, 1.5)) is the same as Matrix().setPos(Vec( 1,-0.5, 1.5)).

Also, anytime you call a Matrix method starting with "set", the matrix is reset, so Matrix().setPos(Vec( 1,-0.5, 1.5)).setRotateY(PI_2) would ignore the positioning altogether.

If Matrix(Vec( 1,-0.5, 1.5)).rotateY(PI_2) doesn't work, you could also try Matrix().setRotateY(PI_2).move(Vec( 1,-0.5, 1.5)).

PI_2 is 90 degrees in radians. rotateY() uses radians, not degrees. You could also use .rotateY(DegToRad(90)) if you don't want to use PI_2.
09-26-2010 07:05 PM
Find all posts by this user Quote this message in a reply
atarax88 Offline
Member

Post: #11
RE: Moving Items
hey thanks again smile the rotate by PI worked fine.
09-26-2010 09:48 PM
Find all posts by this user Quote this message in a reply
Post Reply