Rotate an object by touch

I have an object and I want to rotate it around an outside center by touch. Maybe there is an easy solution for this but I just can’t figure it out. Any and all help will be very much appreciated. Thanks.

I’m not entirely clear on what you want. Can you share some more information, or a picture or two, or an example from somewhere else?

Thanks for taking an interest in my question. I want to click and drag and object and it moves around a center. Sort of like this but the object only moves when I am dragging it.
IilV

Ok, got it. Do you already have code for clicking and dragging objects? Something like this might work:

local dir = vmath.normalize(center - touch_position)
local x = center.x + dir.x * radius
local y = center.y + dir.y * radius
3 Likes
if action.pressed then

    

	local block = ---checking first to see touch is on top of an object
	if not block then
		return
	end

	state.pressed = true
	block.action = {}
	block.action.x = action.x
	block.action.y = action.y

	state.block_picked = block

elseif action.released then

	state.pressed = false
	state.block_picked = nil
else

	local block = state.block_picked

	if block and block.action and state.pressed then
		
	      diff.x = action.x - block.action.x 
	      diff.y = action.y - block.action.y

	      local pos = go.get_position(block.id)
	      go.set_position(vmath.vector3(pos.x+diff.x, pos.y+diff.y, pos.z), block.id)

	      block.action.x  = action.x
	      block.action.y  = action.y

	end

end

this is code for draging the object with the cursor.
Thanks you so much for the code for rotating the object but I can’t seem to use it in my code.

The snippet I shared must be adapted to your situation. For instance the center point around which the object should be rotated. How is the center point defined? Is it the position of some game object or a hardcoded vector3?

The touch_position in my snippet would be:

local touch_position = vmath.vector3(action.x, action.y, 0)

And you would position the dragged object using x and y from my snippet. So it would look something like this:

-- rotate around this position
local center = vmath.vector3(100, 100, 0)

-- the position of the cursor/finger
local touch_position = vmath.vector3(action.x, action.y, 0)

-- the direction from the rotation center and the cursor (-1.0 to 1.0 for x and y)
local dir = vmath.normalize(center - touch_position)

-- position the block at a fixed distance from the rotation point
local x = center.x + dir.x * radius
local y = center.y + dir.y * radius
go.set_position(vmath.vector3(x, y, 0), block.id)
1 Like

I have tried this and the object rotates, but it just jumps to a certain point and then starts rotating. This is where I got stuck on my own too. The initial jump.

Any idea why that might be? Thanks.