Collision wont apply to block

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
renter
Posts: 3
Joined: Tue Mar 03, 2015 3:38 pm

Collision wont apply to block

Post by renter »

So I generated a 50x50 block world and I attempted to apply collision to the blocks. I feel its because of the way Im looping and adding them to the selector that its not working. How should I be doing this? I know the collision is applied because gravity applies to my camera when I turn it on. Also I remember reading something about combining all my meshes together to get better performance. What was that called? Any tips or code comments are much appreciated.

Code: Select all

#include <irrlicht.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h> 
#include <math.h>
 
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
 
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
 
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()
{
    MyEventReceiver keyReciever;
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, dimension2d<u32>(1280, 720), 32, false, false, false, &keyReciever);
    if (!device)
        return 1;
    device->setWindowCaption(L"Voxel type world generator");
    device->getCursorControl()->setVisible(false);
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
 
    //Control Camera with WASD
    SKeyMap controlKeyMap[5];
    controlKeyMap[0].Action = EKA_MOVE_FORWARD;
    controlKeyMap[0].KeyCode = KEY_KEY_W;
    controlKeyMap[1].Action = EKA_STRAFE_LEFT;
    controlKeyMap[1].KeyCode = KEY_KEY_A;
    controlKeyMap[2].Action = EKA_MOVE_BACKWARD;
    controlKeyMap[2].KeyCode = KEY_KEY_S;
    controlKeyMap[3].Action = EKA_STRAFE_RIGHT;
    controlKeyMap[3].KeyCode = KEY_KEY_D;
    controlKeyMap[4].Action = EKA_JUMP_UP;
    controlKeyMap[4].KeyCode = KEY_KEY_F;
    ICameraSceneNode* myCam = smgr->addCameraSceneNodeFPS(0, 50, .2, -1, controlKeyMap, 5);
    myCam->setPosition(vector3df(-50, 0, 0));
 
    //This is our map file and heightmap
    IMeshSceneNode* map[50][50];
    int heightmap[5][5] = { { 0, 1, 2, 3, 4 }, { 0, 0, 1, 2, 3 }, { 1, 0, 0, 1, 2 }, { 2, 1, 0, 0, 1 }, { 3, 2, 1, 1, 0 } };
    
    //This will generate our cubes
    ITriangleSelector* sel;
    int size = 10;
    srand(time(NULL));
    for (int y = 0; y < sizeof(map[0]) / sizeof(*map[0]); y++){
        for (int x = 0; x < sizeof(map) / sizeof(*map); x++){
            map[x][y] = smgr->addCubeSceneNode(10);
            map[x][y]->setPosition(vector3df(x*size, heightmap[x/10][y/10]*5, y*size));
            map[x][y]->setMaterialTexture(0, driver->getTexture("C:/Users/Edward/Pictures/grass.jpg"));
            map[x][y]->setMaterialFlag(EMF_LIGHTING, false);
            
            //Should add our bounds?
            sel = smgr->createOctreeTriangleSelector(map[x][y]->getMesh(), map[x][y]);
            map[x][y]->setTriangleSelector(sel);
        }
    }
 
    scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
        sel, myCam, vector3df(30, 40, 30),
        vector3df(0, 0, 0),//vector3df(0, -30, 0),//Gravity
        vector3df(0, 50, 0));
    myCam->addAnimator(anim);
 
    while (device->run())
    {
        driver->beginScene(true, true, SColor(255, 255, 0, 0));
        if (keyReciever.IsKeyDown(KEY_SPACE)){}
        smgr->drawAll();
        driver->endScene();
    }
 
    device->drop();
 
    return 0;
}
Image
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Collision wont apply to block

Post by CuteAlien »

A few points. Before starting with this project search around in the forum for post that mention minecraft. Basically - the naive approach of just creating lot's of cubes won't work. You only want a mesh for those planes which are visible. But from what I read there are already libs out there helping you to optimize this stuff. Combining cubes together will improve speed ... that is unless you decide to add/remove blocks at certain times in your game. Because then the operation to combine the meshes would have to run again. But just in case you still want it: https://sourceforge.net/p/irrext/code/H ... ene/IMesh/
MeshCombiner and BatchingMesh are both about merging meshes.

Next is - you create lot's of selectors. But only pass the last one to the createCollisionResponseAnimator. So it only collides against the last cube. So you have to choices here - either figure out a way to have a single mesh - or combine your selectors. Combining your selectors can be done with a metatriangleselector (you can add many selectors into that). And you shouldn't use an octree if you all you collide against is a single cube - octree's only optmize things once you have many triangles - for a cube they would just slow things down.

I hope some of this was helpful to you.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply