ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperLicht
forum topic indicator Select 3d objects
person icon
mrdekk
Registered User
Quote
2010-04-05 12:54:48

i have some 3d object in scene. this objects defined by new type of scene node derived from SceneNode. How can i select one of them by mouse?
Thanks for response

person icon
niko
Moderator
Quote
2010-04-05 16:46:20

Hi,
there are multiple possibilities on how to do this.
For all versions, you need to calculate a 3d line from your 2d mouse cursor position, this is done like this (taken from the tutorial):

// calculate the start and end 3d point of the line, the beinning being
// the camera position and the end about 2000 units away in the direction of the
// camera target

var startLine = cam.getAbsolutePosition();
var endLine = startLine.add(cam.getTarget().substract(startLine).multiplyWithScal(2000));


The most simple version would be to calculate a 3d bounding box of your scene node. Simply include all 3d points in a 3d box using code like this:

var box = new Box3d();
for () // all your 3d points
box.addInternalPoint(yourpoint.x, yourpoint.y, yourpoint.z);


And with this box, you can simply test collision using box.intersectsWithLine(startLine, endLine).
Should you have moved your scene node using the .Pos, .Scale or .Rot attributes, you need to transform the 3d box before testing it for collision of course, this is simply possible using yourSceneNode.getAbsolutePosition().transformBoxEx(box);

person icon
niko
Moderator
Quote
2010-04-05 16:50:38

A more precise collision testing, polygon based instead of bounding box based would be by using a TriangleSelector. If you are using indices and vertices to draw your scene node, simply create a Mesh (http://www.ambiera.com/copperlic...) from that, by filling some Meshbuffers and adding them to the mesh. (You can also use this to draw your 3d geometry, btw).
Then, create a a MeshTriangleSelector (http://www.ambiera.com/copperlic...) from it.
Collision then is very simple:

yourtriangleselector.getCollisionPointWithLine(startLine, endLine, true)


person icon
mrdekk
Registered User
Quote
2010-04-05 18:23:00

Many thanks

person icon
pavlost
Registered User
Quote
2010-05-12 00:11:34

&921; am having some troubles selecting scene nodes with mouse click using code. I am loading a scene from CopperCube and then i clone and reposition a mesh from the scene several times. I use :

var startLine = Camera.getAbsolutePosition();
var endLine = get3DPositionFrom2DPosition(CopperLicht.getMouseX, CopperLicht.getMouseY);

and then intersectsWithLine(startLine, endLine)

for each node to test for collision on mouse click. I also tried :

getAbsolutePosition().transformBoxEx(box).intersectsWithLine(startLine, endLine)

None seem to work. Any help please?

P.S I couldnt find any help in the documentation since transformBoxEx() isnt mentioned. Shall i try Irrlicht forums ?

person icon
niko
Moderator
Quote
2010-05-12 13:40:29

The most simple way to react on clicks on scene nodes is by using a animator: http://www.ambiera.com/copperlic...

But to solve your problem: Maybe your problem is that the end Line point is to near to your camera? Simply extend it a bit, that's what I did with the code above:

var endLine = startLine.add(cam.getTarget().substract(startLine).multiplyWithScal(2000));


person icon
pavlost
Registered User
Quote
2010-05-12 16:50:46

Although the animator code looks very simple it doesnt work or i cant make it work. I load the coppercube scene, get my scenenode to a var, add a clickanimator using :


var animator = new Animator(engine.getScene(), engine, myFunction);
mySceneNode.addAnimator(animator);

in my simple example myFunction is just setting scene node visible to false but nothing happens. The only way to made it work was by using CopperCube and calling a javascript function in the action field.

person icon
pavlost
Registered User
Quote
2010-05-13 08:08:17

Here is a sample code and the screenshot of it :
embedded external image
🔎︎




var engine = new CopperLicht('3darea');
var Distance = 30;
var Triliza = new Array();


function main() {

if (!engine.initRenderer())
return;

var scene = new Scene();
engine.addScene(scene);
scene.setBackgroundColor(Core.createColor(1, 0, 0, 0));

Sky = new SkyBoxSceneNode();
Sky.getMaterial(0).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxb0_small.jpg", true);
Sky.getMaterial(1).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxb1_small.jpg", true);
Sky.getMaterial(2).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxb2_small.jpg", true);
Sky.getMaterial(3).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxb3_small.jpg", true);
Sky.getMaterial(4).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxbup_small.jpg", true);
Sky.getMaterial(5).Tex1 = engine.getTextureManager().getTexture("sunset_skyboxbdn_small.jpg", true);
scene.getRootSceneNode().addChild(Sky);

var cam = new CameraSceneNode();
cam.Pos = new Vect3d(0, 0, -200);
cam.setTarget = new Vect3d(0, 0, 0);
var animator = new AnimatorCameraModelViewer(cam, engine);
animator.Radius = 120;
animator.RotateSpeed = 100;
cam.addAnimator(animator);
scene.getRootSceneNode().addChild(cam);
scene.setActiveCamera(cam);

InitCube(scene);

}


function ClickTest () {
alert("Clicked!");
}


function InitCube(scene) {

var z = -Distance;
for (var j = 0; j < 3; j++) {
var x = -Distance;
var y = -Distance;
for (var i = 0; i < 9; i++) {
Triliza[i + 9 * j] = new CubeSceneNode();
Triliza[i + 9 * j].getMaterial(0).Tex1 = engine.getTextureManager().getTexture("crate_wood.jpg", true);
scene.getRootSceneNode().addChild(Triliza[i + 9 * j]);
Triliza[i + 9 * j].Pos = new Vect3d(x, y, z);
x += Distance;
if (x == Distance * 2) {
x = -Distance;
y += Distance;
}
}
z += Distance;
}

var animator = new Animator(engine.getScene(), engine, ClickTest);
for(var i=0;i<27;i++)
Triliza[i].addAnimator(animator);

}


main();




In the InitCube i add an Animator for each node. When i click it nothing happens. Can anyone please tell me what i am doing wrong? Thanks in advance.

person icon
niko
Moderator
Quote
2010-05-13 08:22:30

By just looking through your code, the last part doesn't appear to make much sense. I would change it to this:

update: Ah, the forum remove some word from the code, maybe it wasn't that wrong, see comment below


for(var i=0;i<27;i++)
{
// note: remove the '-' from the On-click-Animator below, the forum software removes the word otherwise
var animator = new On-Click-Animator(engine.getScene(), engine, ClickTest);
Triliza[i].addAnimator(animator);
}


person icon
pavlost
Registered User
Quote
2010-05-13 09:55:52

I put the animator declaration inside loop but still doesnt work emoji icon_sad

I dont think there is a problem with the code. I even tried a very simple scene with only one cube and one ClickAnimator. I didnt got any response with mouse click on it.

person icon
niko
Moderator
Quote
2010-05-14 06:34:58

Ok, I found your problem, CopperLicht had a bug when you did this just the way you do. It's now fixed in version 1.1.1.

person icon
pavlost
Registered User
Quote
2010-05-14 07:33:30

I am glad it was a bug. I was begining to wonder about my IQ emoji icon_monkeyemoji icon_monkey

I'll do it with CopperCube until next version of CopperLicht is out.
Thanks for the responce.

person icon
niko
Moderator
Quote
2010-05-14 07:42:03

Fixed typo above: I meant it's already fixed in version 1.1.1 :)

person icon
pavlost
Registered User
Quote
2010-05-14 08:01:44

I am on 1.1.0 . Downloading 1.1.1 asap emoji icon_grin

Update : It is working BUT i think Animator calls the function for every node that collides with the ray and not only just the function from the first.

person icon
niko
Moderator
Quote
2010-05-16 08:02:00

That's because the animator doesn't know anything about the other cubes. There are several ways to solve this problem:

- You could either solve this by implementing the line-collision code yourself as the code in the beginning of this thread shows.

- Construct your scene using CopperCube instead. The editor will tell copperLicht which items are there in the world.

- Use the MetaTriangleSelector from scene.getCollisionGeometry() and add all the cubes to the world there. That's basically also what CopperCube does.


Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |