Notifications
Clear all

[Closed] Y Axis LookAt

 MZ1

I want to create a lookAt controller for rotation (Y Axis only). I just modified a code from Here:

delete Objects 
Obj = teapot()
Target = box pos:[100,0,100]
completeredraw()

dir = normalize (Target.Pos - Obj.Pos)
vec = normalize Obj.transform[3]
tm = Obj.transform
rtm = angleaxis (acos (dot vec dir)) (normalize (cross dir vec))
Obj.transform = translate (rotate (scalematrix tm.scale) (tm.rotation*rtm)) tm.pos

This code works perfect for me, but I get some error when I use it in script controller.

11 Replies

first of all why the built-in LookAt controller doesn’t work for you?

another question: where do you want to use expression? rotation controller or transform controller?

1 Reply
 MZ1
(@mz1)
Joined: 1 year ago

Posts: 0

it has problem when i mirror(negative scale) obj or target

Rotation (Y)

(
	delete objects
	point name:#s axis:on wirecolor:green
	point name:#t pos:[20,0,0] axis:off wirecolor:yellow

	ss = $s.rotation.controller = createinstance rotation_script

	ss.addobject "pos" $s.position.controller
	ss.addnode "target" $t

	ex = "tm = arbaxis (normalize (pos.value - target.pos))
"
	ex += "prerotateX tm -90
" 
	ex += "tm.rotation"

	ss.setexpression ex
)

as added controls to script controller you can use position subanims if you want.
also you can build angleaxis (as quaternion value) instead of transform with using it’s rotation part (as i do in the example)

1 Reply
 MZ1
(@mz1)
Joined: 1 year ago

Posts: 0

The Obj will be flipped when I move Target in negative X axis, just test it with teapot and you see the problem. Another note is that I need rotation in single Y axis.

of course it flips. because i do nothing for two other axis.

if you need to animate y-rotation make rotation controller as a list with lookat and free eulerXYZ

1 Reply
 MZ1
(@mz1)
Joined: 1 year ago

Posts: 0

doesn’t work either. I founded another code from you in other post, and this is my ugly version with some modification:

	 fn YLookAt Obj Target = 
  	(
  		Orient_Script = rotation_Script()
  		Orient_Script.AddNode "Target" target
  		Orient_Script.AddNode "Obj" Obj
  		St = ""
  		St+="if Target != undefined then
" 
  		St+="(
"
  		St+="if (Parent = Obj.Parent) == undefined 
"
  		St+="then
"
  		St+="v = normalize (Target.pos*(inverse (translate (matrix3 1) Obj.pos)))
"
  		St+="else
"
  		St+="v = Target.pos*(inverse (translate (Parent.transform.rotation as matrix3) Obj.pos))
"
  		St+="ang = atan (v.z/v.x)
"
  		St+="if Obj.Scale.X < 0 then Fac = -1 else Fac = 1
"
  		St+="ang += if v.x < 0 then Fac*90 else Fac*-90
"
  		St+="((rotateYMatrix -ang) as quat)
"
  		St+=")
"
  		St+="else (quat 0 0 0 1)"
  		
  		Orient_Script.SetExpression St
  		
  		Obj.rotation.controller = Orient_Script		
  	)

This code is near to what I want but, has problem when I scale the Obj.

as i guess you have a pair of objects: normal and flipped (scaled with a negative value).
and you want them both look at specific node using Y axis as a direction.

true?

if i guess wrong please show a picture how ‘lookAt’ has to look like for normal and negative scaled cases.

1 Reply
 MZ1
(@mz1)
Joined: 1 year ago

Posts: 0

I wanna use it for rigging 2d (cutout) character. this means character parts will rotate only in one axis. in this kind of rigging flipping every part is necessary. Please see 3 attached image.

it looks like you over-complicate the rig solution.
you can simply solve it using hierarchy only:

upperArm_bone->lowerArm_bone->hand_bone

and
upperArm_bone->upperArm_flesh
lowerArm_bone->lowerArm_flesh
hand_bone->hand_flesh

delete objects
(
	b0 = BoneSys.createBone [0,0,0] [20,0,0] y_axis
	b0.name = "UpperArm_bone"
	b1 = BoneSys.createBone [20,0,0] [40,0,0] y_axis 
	b1.parent = b0
	b1.name = "LowerArm_bone"
	b2 = BoneSys.createBone [40,0,0] [50,0,0] y_axis
	b2.parent = b1
	b2.name = "Hand_bone"
	
	f0 = point name:"UpperArm_flesh" transform:b0.transform parent:b0
	f1 = point name:"LowerArm_flesh" transform:b1.transform parent:b1
	f2 = point name:"Hand_flesh" transform:b2.transform parent:b2
)

bone system gives you true lookAt constraining, and ‘flesh’ controls are free to have own animation. you can lock all axis to preserve their change

1 Reply
 MZ1
(@mz1)
Joined: 1 year ago

Posts: 0

It’s already complicated with IKFK,Skin,Flipping,Moving separately,…
However, I will tell you some problems of your rig:

[ol]
[li]Every mesh is skinned to two bones (pluse one twist bone, orientation constrained to this two bones) and must be flipped locally, How you flip UpperArm bone in X axis while lower part of the mesh is directed to the hand?[/li][li]How I can move just ForeArm in Z axis, but hand stay in it’s position?[/li][/ol]

 MZ1

To see what final rig looks like, please take a look at attached max file. it would be great if you tell me how I can optimized it. Note that to Flip or move in Z, use the shapes with name “*IKFKP”(Red NGones).