# Simple rotation and facing

**URL:** <https://forum.defold.com/t/simple-rotation-and-facing/65681>\
**Category:** The Defoldmine\
**Created:** [June 28, 2020, 6:31pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681 "2020-06-28T18:31:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon64925349](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@anon64925349](https://forum.defold.com/u/anon64925349)\
**Post date:** [June 28, 2020, 6:31pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/1 "2020-06-28T18:31:21Z")

</div>

Hello, here I present you my modest library for Defold, it is used to easily set the rotation of objects only in degrees or to rotate an object towards another object or position. I would be happy if it ever becomes a real part of the Defold.

```
-- This is the code for rotating object in degrees:
rotate(url, degrees)
-- example:
rotate(".", 90)

-- And this is the code for rotating to another object:

rotate(url, url 2)
-- url = url adress of object you want to rotate
-- url 2 = url adress of object you want to rotate to

-- example:
rotate(".", "go1")

```

And there is the full code of my small Lua library:

```
function rotate(from, to)
if tonumber(to) then
	if to == nil then
		to = go.get_id()
	end
	go.set(from, "euler.z", to)
else
	local direction = go.get_position(to) - go.get_position(from)
	local rotation = vmath.quat_rotation_z(math.atan2(direction.y, direction.x))
	go.set_rotation(rotation, from)
end

```

end

I hope this will make development easier for you.  
I wish you happy Defolding.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 29, 2020, 8:11am UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/2 "2020-06-29T08:11:54Z")

</div>

> [@anon64925349](#):
>
> if tonumber(to) then

You can use `if type(to) == "number" then`

---

<div class="post-metadata">

**Author:** ![anon64925349](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@anon64925349](https://forum.defold.com/u/anon64925349)\
**Post date:** [June 29, 2020, 9:16am UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/3 "2020-06-29T09:16:29Z")

</div>

Hello, there is the small update for addition degrees. For example for animating.

```
rotate(url, rotation() + degrees)
-- rotation() = constant

-- this is example of the code
rotate(".", rotation() + 1)

-- this is added code of the library
function rotation(url)
if url == nil then
	return go.get(".", "euler.z")
else
	return go.get(url, "euler.z")
end
end

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 29, 2020, 3:53pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/4 "2020-06-29T15:53:40Z")

</div>

> [@anon64925349](#):
>
> ```
> if url == nil then
> return go.get(".", "euler.z")
> else
> return go.get(url, "euler.z")
> end
> 
> ```

More Lua specific improvements (well, depending on how you look at it but I like this style):

```
return go.get(url or ".", "euler.z")

```

---

<div class="post-metadata">

**Author:** ![Alex\_8BitSkull](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alex_8bitskull/32/5424_2.png) [@Alex\_8BitSkull](https://forum.defold.com/u/Alex_8BitSkull)\
**Post date:** [June 29, 2020, 8:52pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/5 "2020-06-29T20:52:34Z")

</div>

Does this only yield cool points or is it better for performance as well? I pull these things off from time to time but frequently miss the opportunity.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [June 29, 2020, 9:32pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/6 "2020-06-29T21:32:18Z")

</div>

> [@Alex\_8BitSkull](#):
>
> only yield cool points or is it better for performance as well

Mainly cool points and fewer lines of code. Not sure if it matters much performance-wise.

It’s very useful when you want to assign a default value to a variable if the user doesn’t provide it, and it’s something you see in a lot of Lua codebases.

```lua
local function greet(name, greeting)
    greeting = greeting or "Hello"
    print(greeting .. " " .. name)
end

greet("Bob") -- Hello Bob
greet("Bob", "Yo") -- Yo Bob

```

---

<div class="post-metadata">

**Author:** ![rayraytime](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@rayraytime](https://forum.defold.com/u/rayraytime)\
**Post date:** [November 21, 2021, 2:28pm UTC](https://forum.defold.com/t/simple-rotation-and-facing/65681/7 "2021-11-21T14:28:41Z")

</div>

Thanks for this! Put this code to use today.
