Sprite scales up with math.quat_from_to and go.set_rotation

I tried this again with a completely different project and I still get sprites scaling up as their direction nears the opposite.

Here’s the script for the behavior of this

local rendercam = require("rendercam.rendercam")

local YVECT = vmath.vector3(0, 1, 0)
local XVECT = vmath.vector3(1, 0, 0)
local QUAT180 = vmath.quat_rotation_z(math.pi)

local function vec_to_quat_x(vec)
	return vec.x == -1 and QUAT180 or vmath.quat_from_to(XVECT, vec)
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.target = vmath.vector3()
	self.direction = vmath.vector3(1,0,0)
	self.position = go.get_position()
	self.movement = vmath.vector3(1,1,0)
	self.speed = 10
	self.moving = false
	self.basis = vmath.vector3(1,0,0)
end

function final(self)
	-- Add finalization code here
	-- Remove this function if not needed
end

function update(self, dt)
	self.basis = vmath.vector3(1,0,0)
	go.set_rotation(vec_to_quat_x(self.direction))

	if self.moving == true then
		self.movement = vmath.normalize(self.movement)
		self.position = self.position + self.movement * self.speed
		go.set_position(self.position)
	end
	pprint(self.position)
	pprint(self.movement)
	self.moving = false
end

function on_message(self, message_id, message, sender)

end

function on_input(self, action_id, action)
	self.target = rendercam.screen_to_world_2d(action.screen_x, action.screen_y)
	self.direction = vmath.normalize(self.target - self.position)

	if action_id == hash("key_w") then
		self.movement = self.movement + vmath.vector3(0,1,0)
		self.moving = true
	end
	if action_id == hash("key_a") then
		self.movement = self.movement + vmath.vector3(-1,0,0)
		self.moving = true
	end
	if action_id == hash("key_s") then
		self.movement = self.movement + vmath.vector3(0,-1,0)
		self.moving = true
	end
	if action_id == hash("key_d") then
		self.movement = self.movement + vmath.vector3(1,0,0)
		self.moving = true
	end
	
	
end

function on_reload(self)
	-- Add reload-handling code here
	-- Remove this function if not needed
end

I didn’t see you asking about euler solution before but this is what I did which seems to not break.

	self.gun_direction = math.atan2(rendercam_helper.look_direction.y, rendercam_helper.look_direction.x)*180/math.pi
	go.set(self.gun_id, "euler.z",  self.gun_direction)
	
2 Likes