I use a system in which i place an object with regard the plane it is contained into. Basically:
-find the horizontal plane where the object lies.This is easy because you have a point, that is the object location, and the normal is the vertical vector, (0,1,0)
-find the ray where the mouse is pointing at. The collision manager is able to find this ray quickly
-place your object in the intersection between the ray and the plane. Normally, it works quite well and fast enough so it can be done in real time.
-If you want to place your object into another plane, just move it vertically.
In a nutshell, this is pretty much it.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
scene::ISceneNode* node;
node = collMan->getSceneNodeFromScreenCoordinatesBB (mousePosition);
if(node)
{
//-find the horizontal plane where the object lies.This is easy because you have a point, that is the object location, and the normal is the vertical vector, (0,1,0)
core::plane3d plane;
plane.setPlane(node->getLocation(),core::vector3df(0,1,0));
//-find the ray where the mouse is pointing at. The collision manager is able to find this ray quickly
core::line3df ray = collMan->getRayFromScreenCoordinates(mousePosition,sceneManager->getActiveCamera());
//-place your object in the intersection between the ray and the plane.
core::vector3df point;
plane.getIntersectionWithLine(ray.start,ray.end,point);
node->setPosition(point);
}
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
ok but I do not understand 2 things: 1) node in my code corresponds to the node of the block?? 2) MousePosition ... not declared Thanks very much for the help
Sorry, we can't really help you unless you know how to code a little bit more. I just have no idea how we can't help you using a c++ 3d-engine unless you can at least write enough c++ to understand stuff like posted above. You can't do a game and expect other people to write the functions for you, that just doesn't work. Crossposting the same questions over and over in the forum also doesn't help unless you are already knowing enough to actually understand the answers!
Please start with simpler programs than a 3D game, to get really familiar with c++ first. Maybe you could even start by working your way through the Irrlicht examples, but I would rather recommend to start with simpler code. I started myself doing text-only applications for a while before I switched to 2D and then later switched to 3D programming.
I know enough about the c + + but also tell me if I knew the C + + well but did not know the functions of irrlicht. How would I do? Then if I try to learn but nobody can help me...
The problem is we are trying to help you... Mel gave you the core of one solution how to solve a collision with floor. I tried to tell you about how to find the troubles in your other code. But you expect finished solutions and have still trouble with function parameters and undeclared variables. That is a sign that you should work on simpler problems for now. This stuff gets easier after you have coded a few simpler projects. But learning this at the same time while struggling with learning 3d programming - you're making your life hard - and annoy other people with the wrong questions because you skipped too many steps in your learning. You don't see we already told you the hard parts because you are still struggling with the easy parts.
It is typical when you're new to programming to expect seeing complete working functions. But once you start really programming it is up to you to figure out how to write such functions. And yes, this takes long and we try to help you - but we won't _do_ it for you - because figuring out such functions _is_ what a programmer does. You don't ask in the forum after you're stuck a little. You figure it out yourself and ask in a forum once you really have tried everything. You don't post the same code over and over and expect people to write it perfect for you. You write it yourself and add debugging information and use a debugger and read documentation etc. and you spend days and weeks on it. And yes, you have to learn how to add debug-information. And you have to learn how to use a debugger. And you have to learn how to read API documentation. Those are the easy parts with which you have be comfortable to be able to work on harder problems. That is how you learn. And _then_ you are able to ask specific question. Because you can show screenshots with your debug-output. You can tell exactly which parts are causing you trouble and you can tell what the debugger said. And maybe you will even run at some point in a function that does not behave as the documentation said and you can show it with a test-case you wrote. And you don't have to ask for a function which does everything for you anymore - you won't need it because after a few hints you will figure out the rest yourself.
I don't want to de-motivate you. I just want you learning stuff in a order that isn't frustrating to everyone. If you have the feeling you can't continue this despite all the hints you got already then please start with simpler problems. You will learn faster and can get back to this problem once you feel confident you can solve this. No-one (or nearly no-one) just starts out as 3d programmer.
Also from ISceneManager with getSceneCollisionManager you get http://irrlicht.sourceforge.net/docu/cl ... nager.html
There you have functions to get the ray of positions in the 3D world for a 2D mouse position and a given camera position.
1. create a triangle selector and set this selector to your plane mesh node;
2. the intersection will be with the horizontal plane, not with your node;
3. the outnode is the collided node, (not your mesh node to be moved);
DLL_EXPORT void* DLL_APIENTRY nGetNodeAndCollisionPointFromCamera(
ICameraSceneNode* camera,
//ISceneNode *planenode,
ITriangleSelector* selector,
float* xcoll,
float* ycoll,
float* zcoll)
{
//planenode->setTriangleSelector(selector);
//selector->drop();
scene::ISceneCollisionManager* collMan = SceneManager->getSceneCollisionManager();
core::line3d<f32> ray;
ray.start = camera->getPosition();
ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 10000.0f;
core::vector3df intersection;
core::triangle3df hitTriangle;
scene::ISceneNode * outNode =
collMan->getSceneNodeAndCollisionPointFromRay(
ray,
intersection, // This will be the position of the collision
hitTriangle, // This will be the triangle hit in the collision
1<<0, // This ensures that only nodes that we have
// set up to be pickable are considered
0); // Check the entire scene (this is actually the implicit default)
*xcoll=intersection.X;
*ycoll=intersection.Y;
*zcoll=intersection.Z;
return (void*) outNode;
}
then : blocconode->setPosition(intersection); to move your mesh at intersection point with planenode;
void SchootBlocks(){
scene::ITriangleSelector* selector;
scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
irr::core::position2d<s32> mousePosition= device->getCursorControl()->getPosition();
//da modificare la texture blocco in base all blocco che si e selezionato
IAnimatedMesh *blocco=smgr->getMesh("../../media/blocco.3ds");
IAnimatedMeshSceneNode *blocconode=smgr->addAnimatedMeshSceneNode(blocco,smgr->getRootSceneNode());
blocconode->setMaterialFlag(video::EMF_LIGHTING,false);
ISceneNode *planenode;
planenode->setTriangleSelector(selector);
selector->drop();
core::line3d<f32> ray;
ray.start = cam->getPosition();
ray.end = ray.start + (cam->getTarget() - ray.start).normalize() * 10000.0f;
core::vector3df intersection;
core::triangle3df hitTriangle;
scene::ISceneNode * outNode =collMan->getSceneNodeAndCollisionPointFromRay(
ray,
intersection, // This will be the position of the collision
hitTriangle, // This will be the triangle hit in the collision
1<<0, // This ensures that only nodes that we have
// set up to be pickable are considered
0); // Check the entire scene (this is actually the implicit default)
blocconode->setPosition(intersection);
}