terrain vertices square

Posted By: Dega

terrain vertices square - 03/08/15 20:59

I'm trying to make a square appear on my terrain. The square is moved by the mouse but is always snapped to the grid on the terrain which is basically. The corners of the square are the terrains vertices. Really would appreciate any help.

Here is what I got so far and as I predicted it was completely wrong:
Code:
void player_gui(){
	VECTOR target_vertice;
	target_vertice.x = mouse_pos.x;
	target_vertice.y = mouse_pos.y;
	target_vertice.z = 200;
	//CURSOR
	vec_for_screen(target_vertice,camera);
	draw_line3d(target_vertice,vector(0,0,100),100);
	draw_line3d(target_vertice.x + 3,vector(0,0,100),100);
}

Posted By: Reconnoiter

Re: terrain vertices square - 03/08/15 21:11

ent_getvertex is what you are looking for I think (see manual example too);

4x c_trace (for four corners) and ent_getvertex with vertex_num

-edit; maybe using c_trace and hit.vertex alone is enough to get the terrain vertex positions, don't know exactly blush
Posted By: Dega

Re: terrain vertices square - 03/09/15 00:10

Here's what I got but it doesn't work.
Any advice on what to do? (Used a example as a template)

Code:
function mole_gun()
{
	while (1)
	{
		
		VECTOR trace_target, camera_target;
		camera_target.x = mouse_pos.x;
		camera_target.y = mouse_pos.y;
		camera_target.z = 200;
		vec_for_screen(camera_target,camera);
		vec_rotate(camera_target,camera.pan);
		if (c_trace(camera, camera_target, IGNORE_PASSABLE | USE_POLYGON| SCAN_TEXTURE) > 0){\
			if (mouse_left && HIT_TARGET && you && ent_type(you) == 4) // fire onto terrain
			{
				var vertex_num = ent_nextvertex(you,hit.x); 
				CONTACT* c1 = ent_getvertex(you,NULL,vertex_num);
				c1.z += 10; 
				c1.v = NULL; 
				ent_setvertex(you,c1,vertex_num);    
				wait(-0.5); 
			}
		}
		wait(1); 
	}
}

Posted By: Wjbender

Re: terrain vertices square - 03/09/15 08:17

I am not very good mathematical so how I would do it :

divide terrain in to 2d chunk rectangles, divide those boxes in to 2d rectangles based on the size of each cell in the chunk ..(quad tree like)

keep that data/list in memory.

to find the rectangle to draw where the mouse is inside , first determine which chunk rectangle the mouse position is inside , then determine which of the cells inside that chunk ,contains the mouse position.

retrieve the relevant information of that cell,like position/min/max , then draw it ..

jb
Posted By: Superku

Re: terrain vertices square - 03/09/15 10:18

You only need to calculate or know the distance between 2 vertices (in each dimension). Then you don't have to interact with any more vertex or terrain related stuff because of the regular terrain structure. You can do something as follows instead:

VECTOR terrain_min,terrain_max,mouse_terrain_pos;
var terrain_cell_size_x,terrain_cell_size_y,mouse_cell_x,mouse_cell_y;

fill terrain_min and terrain_max appropriately,
calculate terrain_cell_size_x/y via ent_getvertex or something like terrain_cell_size_x = (terrain_max.x-terrain_min.x)/(number of vertices in dimension x - 1);

then now calculate mouse_terrain_pos on the terrain (via c_trace or a line equation) and get the cell ids as follows:
mouse_cell_x = floor((mouse_terrain_pos.x-terrain_min.x)/terrain_cell_size_x); // same for y

You can now use mouse_cell_x/y to draw a quad on the terrain (or place a quad sprite there) and perform further calculations:
quad.x = (mouse_cell_x+0.5)*terrain_cell_size_x; // same for y. 0.5 because I assume quad is a sprite
Posted By: Dega

Re: terrain vertices square - 03/09/15 17:07

@Superku

how to set VECTORS terrain_min and terrain_max. I'm trying to figure out what you're using them for? Also I have the terrain loaded in WED so I was wondering how to figure out how to reference the terrain entity.
Posted By: Superku

Re: terrain vertices square - 03/09/15 17:36

If you only have one terrain the easiest way to address it is to use an action with a pointer:

ENTITY* my_terrain = NULL;

action act_my_terrain()
{
my_terrain = me;
}

############

terrain_min and terrain_max are meant to be the position of the lower left and the upper right corner of the terrain respectively.
I am not 100% sure but I think terrain.min_x/max_x works:

vec_set(terrain_min,my_terrain.min_x);
vec_add(terrain_min,my_terrain.x); // my_terrain.x may be 0 because I think the terrain gets converted to world coordinates
vec_set(terrain_max,my_terrain.max_x);
vec_add(terrain_max,my_terrain.x);

If this does not work you will have to figure out a way how to set those vectors up. You can do it.
Posted By: Dega

Re: terrain vertices square - 03/10/15 19:42

okay so I completely redid the code. I just need to figure out if there is something that returns the vector of a vertex. Also after the terrain is deformed the player can just move through the new part of the ground as if the old ground of the terrain was still there. Any suggestions?

Code:
///Change Terrain
                VECTOR pos1,pos2;
		vec_set( pos1 , vector(mouse_pos.x,mouse_pos.y,0) );
		vec_for_screen (pos1, camera);
		
		vec_set(pos2 , vector(mouse_pos.x,mouse_pos.y,10000));
		vec_for_screen (pos2, camera);
		
		c_trace (pos1.x, pos2.x, IGNORE_ME | IGNORE_CONTENT | IGNORE_SPRITES | IGNORE_MAPS | IGNORE_PASSABLE | IGNORE_MODELS | IGNORE_FLAG2 | SCAN_TEXTURE); 
		
		
		if (HIT_TARGET && you && ent_type(you) == 4) // click onto terrain
		{
			var vertex_num1 = ent_nextvertex(you,hit.x);
			CONTACT* c1 = ent_getvertex(you,NULL,vertex_num1);
			var vertex_num2 = ent_getvertex(you,NULL,vertex_num1 + 1);
			var vertex_num3 = ent_getvertex(you,NULL,vertex_num1 + 66);
			var vertex_num4 = ent_getvertex(you,NULL,vertex_num2 + 66);
			draw_point3d(hit.x,COLOR_RED,100,5);
			if(mouse_left){ 
				c1.z = my.z + my.min_z;  // increase the vertex height
				c1.v = NULL; // c.x,y,z was changed, instead of c.v   
				ent_setvertex(you,c1,vertex_num1);    // update the mesh
				my.z = c1.z - my.min_z - 1;
				wait(-1); // reload
			}
			
		}

Posted By: MasterQ32

Re: terrain vertices square - 03/13/15 11:37

you need to call c_updatehull to perform a collision update hull on the terrain
Posted By: sivan

Re: terrain vertices square - 03/13/15 12:45

I use normally c.v instead of c to avoid that NULL line:

CONTACT* c = ent_getvertex(terrain_entity,NULL,vertexnum);
c.v.y += (float)increase; // float and y, because here we need to use DirectX coordinates,
ent_setvertex(terrain_entity,c,vertexnum);

updatehull is not needed as hull is updated properly from A8.40.3

I would not hardcode vertex quantity, you can use
var verticesx = ent_status(terrain_entity,2)+1;
© 2024 lite-C Forums