COLLISION DETECTION

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
anjanav das
Posts: 5
Joined: Sun Dec 09, 2007 11:13 am
Location: hyderabad

COLLISION DETECTION

Post by anjanav das »

Hi all
am doing one 3D project by using one irrlicht engine,
here is the code an=m using

CODE:
TriangleSelector selector = null;
if (node1 != null)
{

selector=smgr.CreateOctTreeTriangleSelector(mesh2.GetMesh(0), node1, 128);
node1.TriangleSelector = selector;
node.TriangleSelector = selector;
}

Animator anim=smgr.CreateCollisionResponseAnimator(selector,node,new Vector3D(23,50,13),new Vector3D(0,-2,0),new Vector3D(0,5,0),0);
node.AddAnimator(anim);


here node is the scenenode of a 3D mesh DWARF
and node1 is the scenenode of the 3D mesh of a room.
am doing translation ,rotation and scaling operation with the DWARF.
for translation the collision detection is working but for rotation and scaling the collision detection is not working
please suggest me some steps
Thanks & Regards
Anjanav
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sorry, I don't even understand exactly what the problem is.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

anjanav das, The IRRlicht collision system is not using your mesh for the collision, but a collision sphere. Here you defined the size of that sphere:
Vector3D(23,50,13),
So translation will always work, but rotation (if you got arm and your collision sphere is too small, the arm will go thru the wall, the same apply to your scale)

So check the size of your collision sphere if you don't want your object to go thru the walls. A quick recommendation also is to put your sphere as round as you can, so that your object will not get stuck in every obstacle of your level. Being more round, will allow it to slide better on the floor/walls.
anjanav das
Posts: 5
Joined: Sun Dec 09, 2007 11:13 am
Location: hyderabad

Post by anjanav das »

Thanx christian
I got ehat u said i have been trying with the radius before also
I will give it a shot once again

Thaks so much for ur quick reply
Thanks & Regards
Anjanav
anjanav das
Posts: 5
Joined: Sun Dec 09, 2007 11:13 am
Location: hyderabad

Rotation and Collision

Post by anjanav das »

Hi christian
I was working on ur suggestion about the ellipsoid radius,But the problem still persists.Even after the radius is increased to quite a large value the figure of that dwarf is passing through the floor while rotating
the present value for the ellipsoid radius am using is 55,65,55

please suggest if if there is some other problem
Thanks & Regards
Anjanav
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

I still can't figure out what the problem is, let alone the solution. I suggest that you post your source plus any required resource files.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

Hi, anjanav das


Could you please post a screenshot and the current code for your collision response animator?

It will help us figure what is happening.

Also here is a suggestion how I would write it. Rogerborg is it good or bad to write new before vector3d?

Code: Select all

scene::ISceneNodeAnimatorCollisionResponse* anim=smgr->CreateCollisionResponseAnimator(selector,node, 
                                                    vector3df(25.0,50.0,25.0),
                                                    vector3df(0.0,-2.0,0.0),
                                                    vector3df(0.0,5.0,0.0),0.0005f); 
node->addAnimator(anim);
Be careful also to do not scale the dwarf if you convert it in an OCCTREE. I had problem with that.

Are you using C++ or C#?
Last edited by christianclavet on Fri Dec 21, 2007 4:18 pm, edited 1 time in total.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

*puts his Rogerborg hat on*

BAD!

well in this case anyway. If you write new before it then you're allocating memory for it dynamically on the heap but as you're not retaining a reference to it here (it's being passed directly as a parameter, not stored in a pointer first) you can't delete it so it causes a memory leak.

This would be ok:

Code: Select all

vector3df* pVec = new vector3df();
node->setPosition(pVec);
delete pVec;
But it's not necessary as you can just pass it in without the new and it's much less error prone!
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Heh, thanks. I've got a bad case of Friday Brain, so all I'll be able to contribute tonight is "Post source or GTFO." ;)
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
anjanav das
Posts: 5
Joined: Sun Dec 09, 2007 11:13 am
Location: hyderabad

COLISION

Post by anjanav das »

Hello all
I am posting the whole code now.

Code: Select all

namespace WindowsApplication2
{
    public class SSTL3DObject : ISceneNode
    {
        Material Material = new Material();
        SceneManager _mgr;
        VideoDriver _driver;
        private Mesh mMesh = null;

        public SSTL3DObject(SceneNode parent, SceneManager mgr, int id): base(parent, mgr, id)
        {
            _mgr = mgr;
            _driver = _mgr.VideoDriver;
            Material.Lighting = false;
        }

        public void SetColor(Color lColor)
        {
            try
            {
                if (mMesh != null)
                {
                    _mgr.MeshManipulator.SetVertexColors(mMesh, lColor);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception in SSTLObject.SetColor()"+ex.ToString());
            }
        }
        public Color GetColor()
        {
            try
            {
                if (mMesh != null)
                {
                    return mMesh.GetMeshBuffer(0).GetVertex((uint)0).Color;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception in SSTLObject.GetColor()" + ex.ToString());
            }
            return Color.White;
        }

       

    }
    class SSTLEdit
    {
        private SceneNode SelectedObject;
        private Object obj;
        private Color color;

        public SSTLEdit(Object obj)
        {
            try
            {
                this.obj = obj;
              
            }
            catch (Exception ex)
            {

                MessageBox.Show("Exception in SSTLObject.SetObject()" + ex.ToString());
            }
        }
        public void SetObject(SceneNode Object)
        {
            try
            {
                this.SelectedObject = Object;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception in SSTLObject.SetObject()" + ex.ToString());
            }

        }
    }

    public partial class Form1 : Form
    {
        SceneNode room;
        IrrlichtDevice device; 
        SceneManager smgr = null;
        VideoDriver driver = null;
        CameraSceneNode cam;
        
        SceneNode node;
        
        SceneNode node1;
        private bool Translate;
        private bool Scal;
        private bool Rotate;
        private bool paint;
        SceneNode mSelectedObj ;
        private bool ctrlPressed = false;
        
        private string _CurrentTrans = string.Empty;
        private ArrayList _SelectedObjects = new ArrayList();
        private int button = 0;
        private float mousePoint_X;
        private float mousePoint_Y;
        private float mouseX;
        private float mouseY;
        private string axisSelected = string.Empty;
        private float mouseDiffX;
        private float mouseDiffY;
        Mesh lMesh;
        Texture texture = null;
        private Color mPaintColor = new Color();
        
        Vector3D rpos = new Vector3D(1, 0, 0);
        Vector3D upos = new Vector3D(0, 1, 0);
        Vector3D rscale = new Vector3D((float)0.1, 0, 0);
        Vector3D uscale = new Vector3D(0, (float)0.1, 0);
        Vector3D lrotate = new Vector3D(0, 45, 0);
        Vector3D urotate = new Vector3D(45, 0, 0);
        private ColorDialog lColDlg = new ColorDialog();
        Vector3D scale1 = new Vector3D(55,65,55);
        TriangleSelector selector = null;
        TriangleSelector selector1 = null;
      
        private SSTLEdit edit;
        SSTL3DObject mCreationObject = null;
    

        public bool CTRL_KEY
        {
            get
            {
                return this.ctrlPressed;
            }
            set
            {
                this.ctrlPressed = value;
            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
            this.Show();
            InitDevice();
            Render();
        }
       
        private void InitDevice()
        {
            device = new IrrlichtDevice(DriverType.Direct3D9, new Dimension2D(800,800), 32, true, true, true, true, this.Handle);
            device.OnEvent += new OnEventDelegate(device_OnEvent); //We had a simple delegate that will handle every event
            smgr = device.SceneManager;
            driver = device.VideoDriver;
            AnimatedMesh mesh1 = smgr.GetMesh(@"D:\Anjanav\irrlicht-1.3.1\irrlicht-1.3.1\media\dwarf.X");
            
           if (mesh1 != null)
            {
               node = smgr.AddMeshSceneNode(mesh1.GetMesh(0), device.SceneManager.RootSceneNode, -1);
               
               node.Position = new Vector3D(60, 0, 0);
              
             }
            texture = driver.GetTexture(@"D:\Anjanav\irrlicht-1.3.1\irrlicht-1.3.1\media\dwarf.jpg");
            node.SetMaterialTexture(0, texture);
            node.SetMaterialFlag(MaterialFlag.Lighting, false);
            
            if (node!= null)
            {

                selector1 = smgr.CreateOctTreeTriangleSelector(mesh1.GetMesh(0), node, 128);
                node.TriangleSelector = selector1;
            }


            #region ROOM

            AnimatedMesh mesh2 = smgr.GetMesh(@"D:\Anjanav\irrlicht-1.3.1\irrlicht-1.3.1\media\room.3ds");

            AnimatedMeshSceneNode node1 = smgr.AddAnimatedMeshSceneNode(mesh2);

            room = null;
            if (mesh2 != null)
            {
                smgr.MeshManipulator.MakePlanarTextureMapping(mesh2.GetMesh(0), 0.003f);
                Texture colorMap = driver.GetTexture(@"D:\Anjanav\irrlicht-1.3.1\irrlicht-1.3.1\media\rockwall.bmp");

                Mesh tangentMesh = smgr.MeshManipulator.CreateMeshWithTangents(mesh2.GetMesh(0));

                room = smgr.AddMeshSceneNode(tangentMesh, null, 0);
                room.SetMaterialTexture(0, colorMap);
                room.SetMaterialType(MaterialType.ParallaxMapSolid);
                room.SetMaterialFlag(MaterialFlag.Lighting, false);
                room.GetMaterial(0).MaterialTypeParam = 0.02f;// adjust height for parallax effect
                tangentMesh.Dispose();       
            }
            #endregion ROOM
       
               
                

                   
            
            
            if (node1 != null)
            {

                selector = smgr.CreateOctTreeTriangleSelector(mesh2.GetMesh(0), node1, 128);
                
                node1.TriangleSelector = selector;
                
            }


            
            cam = smgr.AddCameraSceneNodeFPS(null, 0.9f, 0.9f, true);
            
            cam.Position = new Vector3D(-100, 50, -50);
           
            cam.Target = new Vector3D(0, 0, 0);
           

            Animator anim = smgr.CreateCollisionResponseAnimator(selector, node, scale1, new Vector3D(0, 0, 0), new Vector3D(0, 10, 0), 0.0005f);
            node.AddAnimator(anim);
            Animator anim1 = smgr.CreateCollisionResponseAnimator(selector,cam,new Vector3D(23,50,9),new Vector3D(0,0,0),new Vector3D(0,0,0),0.0f);
            cam.AddAnimator(anim1);
            

            
              
            
          }
            
         public void Render()
        {
           
            while (device.Run())
            {
                
                driver.BeginScene(true, true, Color.TransparentGray);
                smgr.DrawAll();


                
                driver.EndScene();
              }
        }
     
        public bool device_OnEvent(Event ev)
        {
           
          if (ev.Type == EventType.MouseInputEvent)
          {
             
              
                switch (ev.MouseInputEvent)
                {
                    case MouseInputEvent.LMousePressedDown:
                        {
                            button = LBUTTON.DOWN;
                            mSelectedObj = Pick();
                            if (paint)
                            {
                                mSelectedObj = Pick();
                                if (mSelectedObj != null)
                                {
                                    Color color = Color.White;
                                    SetPaintColor(lColDlg.Color);
                                    MessageBox.Show(mSelectedObj.ToString());
                                    edit.SetObject(((SSTL3DObject)mSelectedObj));
                                    color = ((SSTL3DObject)mSelectedObj).GetColor();
                                    ((SSTL3DObject)mSelectedObj).SetColor(mPaintColor); ;
                                    
                                }
                            }
                           
                           
                            OnMouseDown(ev.MousePosition.X, ev.MousePosition.Y, button);
                           
                         }
                        break;
                    case MouseInputEvent.MouseMoved:
                        {
                            if ((Translate || Scal || Rotate) && button == LBUTTON .DOWN)
                            OnMouseMove(ev.MousePosition.X, ev.MousePosition.Y, button);
                        }
                        break;
                             
                       case MouseInputEvent.LMouseLeftUp:
                        {
                            button =LBUTTON.UP;
                           SetDevice(ref this.device);
                        }
                        break;     
                      }

            }
                      
       if (ev.Type == EventType.KeyInputEvent)
            {
                if (Translate || Scal || Rotate)
                {
                    if (ev.KeyCode == KeyCode.Up)
                        up();
                    else if (ev.KeyCode == KeyCode.Down)
                        down();
                    else if (ev.KeyCode == KeyCode.Left)
                        left();
                    else if (ev.KeyCode == KeyCode.Right)
                        right();
                }
                if (ev.KeyCode == KeyCode.Control)
                
                    CTRL_KEY = true;
               
                else
                    CTRL_KEY = false;
            }
            return false;
        }

        public void SetPaintColor(System.Drawing.Color lColor) 
         {
            mPaintColor.A = lColor.A;
            mPaintColor.R = lColor.R;
            mPaintColor.G = lColor.G;
            mPaintColor.B = lColor.B;

            MessageBox.Show("" + lColor);
        }
            

        private void up()
        {
            if (Translate)
                node.Position += upos;
            else if (Scal)
                node.Scale += uscale;
            else if (Rotate)
                node.Rotation += urotate;
        }

        private void down()
        {
            if (Translate)
                node.Position -= upos;
            else if (Scal)
                node.Scale -= uscale;
            else if (Rotate)
                node.Rotation -= urotate;
        }
        private void left()
        {
            if (Translate)
                node.Position -= rpos;
            else if (Scal)
                node.Scale -= rscale;
            else if (Rotate)
                node.Rotation -= lrotate;
        }
        private void right()
        {
            if (Translate)
                node.Position += rpos;
            else if (Scal)
                node.Scale += rscale;
            else if (Rotate)
                node.Rotation += lrotate;
        }


        private SceneNode Pick()
        {
            Line3D line = new Line3D();

            Position2D pos =device.CursorControl.Position;
            pos.X = pos.X + 3;
            pos.Y = pos.Y + 10;
            line = device.SceneManager.CollisionManager.GetRayFromScreenCoordinates(pos, device.SceneManager.ActiveCamera);
           
            SceneNode lSceneNode;
            lSceneNode = device.SceneManager.CollisionManager.GetSceneNodeFromRay(line);
            
            return lSceneNode;
            }
         public void OnMouseDown(int x, int y, int button)
         {
          mousePoint_X = x;
            mousePoint_Y = y;
            this.mouseX = x;
            this.mouseY = y;
          } 
       public void OnMouseMove(float x, float y, int button)
        {
            if (button == LBUTTON.DOWN)
            {
                mouseDiffX = x - mousePoint_X;
                mouseDiffY = y - mousePoint_Y;
                mouseX = x + 100;
                mouseY = y + 100;

                if (Translate)
                    MTranslate();

                if (Scal)
                    MScale();

                if (Rotate)
                    MRotate();
            }  
           
            mousePoint_X = x;
            mousePoint_Y = y;

        }    
            
        public CameraSceneNode GetCamera()
        {
            return cam;
        }

        public void MScale()
        {
            //Vector3D scale1 = new Vector3D(55, 65, 55);
              Vector3D scale;
              if (mSelectedObj != node1)
              {
                  if (this.ctrlPressed)
                  {
                      scale = new Vector3D(((mouseDiffX) / 20), (mouseDiffY / 20), (mouseDiffY / 20));
                      scale1 = scale;
                      
                  }
                  else
                  {
                      scale = new Vector3D(((mouseDiffX) / 20), (mouseDiffY / 20), (mouseDiffY / 20));
                      scale1 = scale;
                      
                  }
              }
              else
              {
                  
              }
          
         }
       
        public void MRotate()
        {
            if (mSelectedObj != node1)
            {
                Material mat = new Material();
                mat.Lighting = false;
                mat.DiffuseColor = Color.Purple;
                driver.SetMaterial(mat);
                Vector3D rotate;
                if (this.ctrlPressed)
                    rotate = new Vector3D(0, (mouseDiffY), 0);
                else
                    rotate = new Vector3D(-(mouseDiffX), 0, 0);


            }
            else
            {
            }

        }
        public void MTranslate()
        {
            if (mSelectedObj == node)
            {
                Vector3D cam = smgr.ActiveCamera.AbsolutePosition;
                Vector3D target = smgr.ActiveCamera.Target;
                Vector3D up = smgr.ActiveCamera.UpVector;
                Vector3D view = (target - cam);
                view.Normalize();
                Vector3D X = up.CrossProduct(view);
                Vector3D pos = mSelectedObj.Position;
                pos += (X * mouseDiffX * 1);
                Vector3D Y = X.CrossProduct(up);
                pos -= (Y * mouseDiffY * 1);
                mSelectedObj.Position = pos;
            }
            else
            {
            }

        }

        #region TOOL STRIP
        private void translateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Translate = true;
            Scal = false;
            Rotate = false;
        }
      
        private void scaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Scal = true;
            Translate = false;
            Rotate = false;
        }
        private void rotateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Rotate = true;
            Scal = false;
            Translate = false;

        }
        #endregion TOOL STRIP

        public struct LBUTTON
        {
            public static int DOWN = -1;
            public static int UP = 1;
        }
        public void SetDevice(ref IrrlichtDevice device)
        {

            this.device = device;
        }

        private void paint_click(object sender, EventArgs e)
        {
            paint = true;
            lColDlg.ShowDialog();
      
           


        }
      

        private void menuStrip1_Click(object sender, EventArgs e)
        {

        }
        
        
    }

    class SelectedObject
    {
      
        public SceneNode _SelectedObj = null;
    }

    
}              


am sorry about the images i cant post it over here but i can say u that the dwarf is staying inside the walls ,and when i am scalling it its hand are only going outside not the body,but in case of rotation the dwarf is rotating through FLOOR
i was trying OCCTREE

Code: Select all

selector = smgr.CreateOctTreeTriangleSelector(mesh2.GetMesh(0), node1, 128);
but changing that line to this one also not helping the rotation

Code: Select all

selector = smgr.CreateTriangleSelector(mesh2.GetMesh(0),node1)
Thanks & Regards
Anjanav
Post Reply