Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualKaze

Posted 13 April 2013 - 04:33 PM

This is in unity but uses vectors and quaternions that should work the same in most libraries.

 

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

 

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html

 

I want to make a bone look at a target point but have two problems.

 

1: unity uses z+ forward and blender uses x-

 

2: the look at should avoid excessive twisting of the bone.

 

public class BoneLookAt : MonoBehaviour {
	

	
	// Use this for initialization
	void Start () {
		//save rest data
		restRot = this.transform.localRotation;
		
			//filter input
		Foward.Normalize();
		FowardQuaternionInverse = Quaternion.Inverse( Quaternion.LookRotation(Foward));
		
		
		
		
	}
	public Vector3 Foward = new Vector3(-1,0,0);
	private Quaternion FowardQuaternionInverse;
	
	private Quaternion restRot;
	
	public Vector3 GetTargetPosition(){
		return this.transform.position +  (this.transform.parent.rotation * restRot) * Foward;
	}
	public Quaternion LookAt(Vector3 v){
		//,transform.InverseTransformDirection(Vector3.up)
		 return  Quaternion.LookRotation(v-this.transform.position) * FowardQuaternionInverse;
		
	}
	
	private Vector3 ang = new Vector3();

	void Update () {
		Debug.DrawLine(this.transform.position,this.transform.position + this.transform.rotation * Foward);
		
		
		
		
		//
		this.transform.rotation = LookAt(GetTargetPosition());
		

		
		
		
		
	}
}


#1Kaze

Posted 13 April 2013 - 04:33 PM

This is in unity but uses vectors and quaternions that should work the same in most libraries.

 

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

 

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html

 

I want to make a bone look at a target point but have two problems.

 

1: unity uses z+ forward and blender uses x-

 

2: the look at should avoid excessive twisting of the bone.

 

public class BoneLookAt : MonoBehaviour {
	

	
	// Use this for initialization
	void Start () {
		//save rest data
		restRot = this.transform.localRotation;
		
		//find up
		//Up = this.transform.rotation * Vector3.up;
			//filter input
		Foward.Normalize();
		FowardQuaternionInverse = Quaternion.Inverse( Quaternion.LookRotation(Foward));
		
		
		
		
	}
	public Vector3 Foward = new Vector3(-1,0,0);
	private Vector3 Up;
	private Quaternion FowardQuaternionInverse;
	
	private Quaternion restRot;
	
	public Vector3 GetTargetPosition(){
		return this.transform.position +  (this.transform.parent.rotation * restRot) * Foward;
	}
	public Quaternion LookAt(Vector3 v){
		//,transform.InverseTransformDirection(Vector3.up)
		 return  Quaternion.LookRotation(v-this.transform.position) * FowardQuaternionInverse;
		
	}
	
	private Vector3 ang = new Vector3();

	void Update () {
		Debug.DrawLine(this.transform.position,this.transform.position + this.transform.rotation * Foward);
		
		
		
		
		//
		this.transform.rotation = LookAt(GetTargetPosition());
		

		
		
		
		
	}
}

 

 


PARTNERS