Labels

i am trying to find efficient way to get labels working for objects in my game. i hope others will find it useful.

i attached the image to make things clear in what i am trying to do.

Uploaded with ImageShack.us

so, let us say that i have four objects in the scene and they are placed around origin (0,0,0), when i click on any of those objects i want to get the label that will explain to me what that object is. i figured out that the best way to make labels is to have the line (that will be rendered with opengl calls) that will point from object to the Rect that will have all the information bound to the given object. direction of the line will be determined by the vector direction made out of the origin point and the point where the object is in the space. also point where the line will connect to the rect should be calculated in what quadrant object is (negative or positive x and y axis)… that is the idea, probably will improve it later.

i would like it to be class that i could reuse in other projects. i started to write the code, but i can’t figure out what is the best practice.

here is what i know:

i can get transfor of the current object from camera class like this:

  var currentTransform : Transform=cam.currentTarget;

this will be used for calculating end point of the line (first one is the objects position) based on the vector direction from origin to the object.

-how to get the camera reference inside the class?
-how to check in what quadrant the object is?
-how to use the class and where the class object should be instantiated? in the camera of GUI scripts?

here is the base of the class i got so far (not really much, i am beginner in these types of things as you can see) any help is appreciated.

//REFERENCE TO CAMERA
var cam : MainCamera;

class Labels extends Object{

  var bToRender : boolean = false;
  var startPosition : Vector3;
  var endPosition : Vector3;
	
	
	//INITIALIZATION FUNCTION THAT WILL 
	//GET THE CAMERA REFERENCE
	function Initialize(){
	
	
	}
		
	function ShowLabel(){
	
	}
	
	function CalculateHandlePoints(){
	
	}
	
	function DrawHandle(){
	
	
	}
	
	
	//THIS FUNCTION ACTUALLY RENDERS
	//EVERYTHING
	function OnPostRender(){	
		
		if(bToRender){
		
			CreateLineMaterial(); 
			lineMaterial.SetPass(0); 
			
				GL.Begin(GL.LINES); 
				   GL.Color(Color(1,1,1,1)); 
				   GL.Vertex3(startPosition.x ,startPosition.y, startPosition.z);
				      GL.Vertex3(endPosition.y ,endPosition.y,endPosition.z); 
            GL.End();
  }
 }
}

any ideas?

Checks whether a position x and y are inferior or superior to Screen.width * 0.5, or Screen.height * 0.5

var verticalSection : String = position.y < Screen.height * 0.5 ? “bottom” : “top”;
var horizontalSection : String = position.x < Screen.width * 0.5 ? “left” : “right”;

I don’t understand what you mean by camera reference. Did you mean Camera.main ?

i figured out that i dont need to explicitly determine in what quadrant we are since i am calculating the end point of the label line from the origin point (0,0,0) and position of the object.

so basically what i need now is to calculate the direction of vector from origin and position of the object and then to use it on the object position to get the end point of the line…

does this makes any sense?

Get the position at the screen of the game object.

Get the position of the center of the screen (Screen width and height / 2).

Substract the former vector by the latter. You get your direction.

currently i am working on the 3d points to get them right first then i will project them on the screen.

i managed to get the direction of the vector:

  var direction : Vector3 = target.position - origin;
  direction.Normalize();
		
  startPoint=target.position;
  endPoint=direction * 2;

i didnt know how to get the offset from the start point so i multiplied it by 2

i noticed that as far as the object is from the origin the shorter distance is from start and end point

i will have to investigate this further

When you say the offset, you mean the length between origin and startPoint ? It is magnitude, isn’t it ? Vector3.magnitude

first, thanks for looking into this.

by offset, i was refering to distance from start and end point. i am currently calculating that distance by simply multyplying the direction vector by 2, that is not good. that way i get short lines for objects that are distant from the origin and long lines for the ones that are closer to it. i would like to have a constant distance from start and end point. i guess adding to direction vector would solve that problem, but i dont know how to do it…

I think you should write :

endPoint = startPoint + direction * 2