Jump to content

how to draw a 3d line in the world?


photo

Recommended Posts

Dear support,

 

I'm now trying to draw a line based on two points.

the two points are set by the mouse, it means that when I moving the mouse on a object, it should feedback the intersected point between the mouse cursor and the surface.

Namely, I need to draw a point a surface_based, it is samely to the way to generate the obstanle, I find the center of  obstacle is automatic linked to the surfaces when I moving the mouse.

 

Could you give me some ideas? thank you very much!

 

 

Best regards,

Joshua

Link to comment

Use ObjectMesh::getIntersection() for finding the surface intersection point (return value ret[0]). The intersection test line segment p0-p1 coordinates must be calculated appropriate for your use case in local mesh space.

 

As the returned intersection point is also in local coordinates it has to be transformed to world coordinates and than finally you can use engine.visualizer.renderLine3D() for feedback line rendering (visualizer rendering must be enabled by engine.visualizer.setEnabled()).

 

Some code snippets (incomplete):

// p0 and p1 line segment for mesh intersection testing in LOCAL mesh coordinates
// based on transformed mouse coodinate
.....
.....

if( m_mesh.getIntersection( p0, p1, 0, ret ) != 0 )
{
       mat4 worldSpace = m_mesh.getWorldTransform();
            
       vec3 p0w = worldSpace *   ret[0];
       vec3 p1w = worldSpace * ( ret[0] + ret[1] );
            
       engine.visualizer.renderLine3D( p0w, p1w, vec4(1.0f,0.0f,0.0f,1.0f) );
}

Depending on your specific requirements you might use engine.world.getIntersection() instead for intersection point determination. I think in this case the returned surface intersection coordinate is returned in world space (but I am not 100% sure, you have to test it)  

Link to comment

Use ObjectMesh::getIntersection() for finding the surface intersection point (return value ret[0]). The intersection test line segment p0-p1 coordinates must be calculated appropriate for your use case in local mesh space.

 

As the returned intersection point is also in local coordinates it has to be transformed to world coordinates and than finally you can use engine.visualizer.renderLine3D() for feedback line rendering (visualizer rendering must be enabled by engine.visualizer.setEnabled()).

 

Some code snippets (incomplete):

// p0 and p1 line segment for mesh intersection testing in LOCAL mesh coordinates
// based on transformed mouse coodinate
.....
.....

if( m_mesh.getIntersection( p0, p1, 0, ret ) != 0 )
{
       mat4 worldSpace = m_mesh.getWorldTransform();
            
       vec3 p0w = worldSpace *   ret[0];
       vec3 p1w = worldSpace * ( ret[0] + ret[1] );
            
       engine.visualizer.renderLine3D( p0w, p1w, vec4(1.0f,0.0f,0.0f,1.0f) );
}

Depending on your specific requirements you might use engine.world.getIntersection() instead for intersection point determination. I think in this case the returned surface intersection coordinate is returned in world space (but I am not 100% sure, you have to test it)  

 

Dear Ulf,

 

  Thank you for your answer! I get the idea from you, but now I feel difficulte to get the point in the world space.

               1. how to translate the screen mouse cursor point to the 3D point ? I only find the  there is a function named getScreenPosition ,but I cann't find something like getscreen to world.

               2. here what the meaning of p0 and p1? how to define them?

 Could you show me detail way to get the intersection point of mouse cursor on the surface?

Thank you again for this.

 

Best regards,

Joshua

Link to comment

Hi Joshua,

 

1. just take a look at <UnigineDir>/data/core/scripts/utils.h. There is the function "getPlayerMouseDirection", which fits perefectly for your needs.

2. I have used the following code to intersect with my objects into the world:

Vec3 p0,p1;
int ret[0];

Unigine::getPlayerMouseDirection(p0,p1);
Object tempMesh = engine.world.getIntersection(p0,p1,~0,ret);	//intersect with objects in my world

if (tempMesh == NULL)
	return;

//your code here

ret[0] holds the position, where the intersection takes place and ret[1] stores the normal of the intersection point. There are many different getIntersection-functions. For example, you can exclude objects you don't want to interesect with. But please keep in mind: If you used the intersection-function once per update and you also have some widgets on your screen, be sure you first checked, if the mouse is over any widget or not.

Link to comment

Hi,

 

Please, take note that getPlayerMouseDirection(p0,p1) will return correct result for object that was not rotated or moved from its original world position. Maybe this post will also be useful in your case.

 

Thanks!

 

Dear sir,

 

Your case is very useful for me .thank you.

But here I have a additional question: As the code shows, you create a new node named dynmesh. and then the getintersection is based on this dynmesh.

int init() {
	dynMesh = add_editor(Unigine::createPlane(1.0f,1.0f,1));
	dynMesh.setWorldTransform(Mat4_identity);
	dynMesh.setMaterial("mesh_base","*");
	dynMesh.setProperty("surface_base","*");
	dynMesh.setName("_NewPlane");
 ...
}
int update() {
    Vec3 p0, p1;
    dynMesh.updateBounds();
    getPlayerMouseDirection(p0,p1,dynMesh.getWorldTransform());
    
    int ret[0];
    int ddd = dynMesh.getIntersection(p0,p1,0,ret);
    if(ddd) log.message("intersected triangle number:%d \n",ret[0]);
    
    return 1;
}


But what I want to do is : I clone the node in your world and move it to another place(directly in the editor, not in the cpp code), now I can get the intersection for the orginal node(surface), but I cann't get the intersection with the new node(surface).   How to get the intersection with unknown node?

 

The case of mine is: there is a complex world ,I want to get the first intersection point(first intersected object).

 

Best regards,

Link to comment
  • 2 weeks later...

Hi,

 

The problem is that intersection detection is working in world script and you clone objects in editor script, so world script doesn't know about this changes at all. Better way would be performing nodes clone operation in world script, so the changes would available to intersection detection immediately.

 

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment
×
×
  • Create New...