Trying to get object that follows camera

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Nash94
Posts: 20
Joined: Fri Aug 31, 2012 3:21 pm

Trying to get object that follows camera

Post by Nash94 »

I'm trying to put some of the tutorials together that creates a node that follows the camera, in a Quake 3 map but the node that i create the "sphere" doesn't even show up.
Any ideas to what i've done wrong do i even have the right idea about it?
all i get is a FPS camera in the quake 3 map.

Code: Select all

// Map camera object loads a Q3 map with a sphere node infront of the camera
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib, "Irrlicht.lib")
#endif
 
#include <irrlicht.h>
#include "driverChoice.h"
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
public:
    // This is the one method that we have to implement
    virtual bool OnEvent(const SEvent& event)
    {
        // Remember whether each key is down or up
        if (event.EventType == irr::EET_KEY_INPUT_EVENT)
            KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
        return false;
    }
 
    // This is used to check whether a key is being held down
    virtual bool IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
    
    MyEventReceiver()
    {
        for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
    }
 
private:
    // We use this array to store the current state of each key
    bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
int main () 
 
{
    // ask user for driver
    video::E_DRIVER_TYPE driverType=driverChoiceConsole();
    if (driverType==video::EDT_COUNT)
        return 1;
 
    // create device
    MyEventReceiver receiver;
 
    IrrlichtDevice* device = createDevice(driverType,
            core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
 
    if (device == 0)
        return 1; // could not create selected driver.
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
   // load map
    device->getFileSystem()->addZipFileArchive("C:/media/map-20kdm2.pk3");
    // create map object mesh
    scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
    scene::ISceneNode* node = 0;
 
    if (mesh)
        node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
    if (node)
        node->setPosition(core::vector3df(-1300,-144,-1249));
// add camera 
    smgr->addCameraSceneNodeFPS();
// cursor off
    device->getCursorControl()->setVisible(false);
 
// draw sphere node
    scene::ISceneNode * Sphere = smgr->addSphereSceneNode();
    if (Sphere)
    {
        Sphere->setPosition(core::vector3df(-1310,-154,-1249));
        Sphere->setMaterialTexture(0, driver->getTexture("C:/media/wall.bmp"));
        Sphere->setMaterialFlag(video::EMF_LIGHTING, false);
    }
 
    int lastFPS = -1;
    u32 then = device->getTimer()->getTime();
 
    const f32 MOVEMENT_SPEED = 5.f;
 
    while(device->run())
    {
        // Work out a frame delta time.
        const u32 now = device->getTimer()->getTime();
        const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
        then = now;
        core::vector3df nodePosition = node->getPosition();
     /*int X = node->getPosition().X;
     int Y = node->getPosition().Y;
     int Z = node->getPosition().Z; */
        Sphere->setPosition(nodePosition);
        
        driver->beginScene(true, true, video::SColor(255,113,113,133));
 
        smgr->drawAll(); // draw the 3d scene
        device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo)
 
        driver->endScene();
 
        int fps = driver->getFPS();
 
        if (lastFPS != fps)
        {
            core::stringw tmp(L"Movement Example - Irrlicht Engine [");
            tmp += driver->getName();
            tmp += L"] fps: ";
            tmp += fps;
 
            device->setWindowCaption(tmp.c_str());
            lastFPS = fps;
        }
    }
 
    /*
    In the end, delete the Irrlicht device.
    */
    device->drop();
    
    return 0;
}
 
 
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Re: Trying to get object that follows camera

Post by greenya »

You need to save camera pointer:

Code: Select all

ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS();
For shpere you need to pass camera pointer as "parent":

Code: Select all

Sphere->setParent(camera);
And now position of the sphere is relative to camera position, so next line:

Code: Select all

Sphere->setPosition(core::vector3df(-1310,-154,-1249));
should has some values like "0, 0, -100"
Nash94
Posts: 20
Joined: Fri Aug 31, 2012 3:21 pm

Re: Trying to get object that follows camera

Post by Nash94 »

Thx that would work great, but the sphere node still wont show up on the map when i complie and run it

I did it without the map and it worked fine.. used a cube as reference point.
Nash94
Posts: 20
Joined: Fri Aug 31, 2012 3:21 pm

Re: Trying to get object that follows camera

Post by Nash94 »

Okay next step is to create an object that follows camera with bullet physics..

so say i got a function

Code: Select all

void CreateSphere(const btVector3 &TPosition, btScalar TRadius, btScalar TMass) {
    
    ISceneNode *Node = irrScene->addSphereSceneNode(TRadius, 32);
    Node->setMaterialFlag(EMF_LIGHTING, 1);
    Node->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
    Node->setMaterialTexture(0, irrDriver->getTexture("ice0.jpg"));
 
    // Set the initial position of the object
    btTransform Transform;
    Transform.setIdentity();
    Transform.setOrigin(TPosition);
 
    btDefaultMotionState *MotionState = new btDefaultMotionState(Transform);
 
    // Create the shape
    btCollisionShape *Shape = new btSphereShape(TRadius);
 
    // Add mass
    btVector3 LocalInertia;
    Shape->calculateLocalInertia(TMass, LocalInertia);
 
    // Create the rigid body object
    btRigidBody *RigidBody = new btRigidBody(TMass, MotionState, Shape, LocalInertia);
 
    // Store a pointer to the irrlicht node so we can update it later
    RigidBody->setUserPointer((void *)(Node));
 
    // Add it to the world
    World->addRigidBody(RigidBody);
    Objects.push_back(RigidBody);
}

now i can call the CreateSphere() function to create a sphere with bullet physics but is there a way to add a pointer to each occasion of this function
so each time i call this i can set it a different pointer? ( kind of like giving each object created an id)
Just need help naming one pointer i can figure the rest out from
Post Reply