#Transform starts bugging out when on a wall

1 messages · Page 1 of 1 (latest)

quiet ore
#

so I am trying to make a building gun. and it works perfectly fine when I am pointing it at a flat surface with the normal 0,1,0 but at ceartain angles it just... starts spinning, bugging out and just in general not working as well

#
                    var point: Vector3 = cast.get_collision_point()
                    var normal = cast.get_collision_normal()
                    var offset: Vector3 = normal * ((bb.size.y/2) + 0.01)
                    var trans = Transform3D(
                        Basis(quaternion_from_two_points(point, point+offset)),
                        point
                    )
                    var rot = _prefab.transform
                    var angle = rot.basis.y.angle_to(normal)
                    var axis = rot.basis.y.cross(normal).normalized()
                    rot = rot.rotated(axis,angle)
                    DebugDraw3D.draw_line(point, point + rot.basis.y*2, Color.RED)
                    DebugDraw3D.draw_line(point, point + main_hand.basis.z * 2, Color.YELLOW)
                    var fixed = rot.basis.get_euler().y
                    var fixed2 = main_hand.basis.get_euler().z
                    var hand_miss = fixed2 - fixed
                    _prefab.transform = rot.rotated(normal,hand_miss)
                    _prefab.position = point+offset

                    DebugDraw3D.draw_line(point, point + normal * 2,Color.BLUE)
                    print(fixed)
                    print(fixed2)
                    print(rad_to_deg(hand_miss))

quiet ore
#

does anyone have any idea why it spins

quiet ore
#

so I have updated-ish the script

#

so basically the issue is that while on flat ground rotation works perfectly
but since rotation is based on the "global basis" it doesn't use the local basis
which means that it cannot properly "allign" it's self

                    var hand_miss = fixed2 - _prefab.rotation.y
                    DebugDraw3D.draw_line(point, point + _prefab.basis.y * 2, Color.BLACK)
                    DebugDraw3D.draw_line(point, point + _prefab.basis.z * 2, Color.BLUE)
                    
                    print("hand_rotation: %s" % deg_to_rad(fixed2))
                    print("prop_rotation: %s" % _prefab.rotation_degrees.y)
                    print("error:         %s" % hand_miss)
                    _prefab.transform = _prefab.transform.rotated(normal,hand_miss)
                    print("prop_port trans: %s" % _prefab.rotation_degrees.y)
                    _prefab.position = point+offset
#

so that leads me to my question. is there any way to make it so the rotation is in basis-orientation

warm hill
#

You can use quaternion multiplication to apply surface normal on a local rotation eg.

var angle := (Time.get_ticks_msec() % 1000) / 1000.0 * TAU

var q_normal := Quaternion(Vector3.UP, hit.normal)
var q_local := Quaternion(Vector3.UP, angle)

node.transform.basis = Basis(q_normal * q_local)
node.transform.origin = hit.position + hit.normal * 0.5
quiet ore
warm hill
#

Yes, that's pretty much it. I just used a simple time based angle because I don't have a vr set to easily control it 😅