### Author Topic: [Solved]How do I make a circle movement?  (Read 309 times)

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### [Solved]How do I make a circle movement?
« on: April 02, 2017, 10:51:06 am »
I am having trouble with this because my npc is not making a circle movement. I want the NPC (gerf) to circle around a entity (chest1).

Code: Lua
1. local circle = sol.movement.create("circle")
2. circle:set_center(chest1,248,357)
3. circle:start(gerf)
« Last Edit: May 06, 2017, 03:27:11 am by zutokaza »

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #1 on: April 02, 2017, 11:23:31 am »
Did you try to add the remaining properties (radius, angle speed, etc) before starting the movement?
I think the Lua API should give an example for each movement.

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### Re: How do I make a circle movement?
« Reply #2 on: April 02, 2017, 07:07:32 pm »
It is still just standing there. No error outputs.

Code: Lua
1. local circle = sol.movement.create("circle")
2. circle:set_center(chest1,248,357)
3. circle:set_loop_delay(2000)
4. circle:set_angle_speed(200)
5. circle:set_duration(2000)
6. circle:set_max_rotations(10)
7. circle:set_initial_angle(360)
8. circle:set_clockwise(true)
11. circle:start(gerf)

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #3 on: April 02, 2017, 07:24:30 pm »
This is weird. Does it happen with other types of movements? And are you sure this code is being called? Maybe posting all the code of the script will help.

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #4 on: April 02, 2017, 07:27:46 pm »
Also, make sure that your NPC is not initially stuck in a wall, heheh.

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### Re: How do I make a circle movement?
« Reply #5 on: April 02, 2017, 08:13:24 pm »
The NPC works with random_path and jump, so it is not stuck.

Full code:
Code: Lua
1. local map = ...
2. local game = map:get_game()
3. local hero = map:get_hero()
4.
5. function map:on_started()
6.
7.   local circle = sol.movement.create("circle")
8.    circle:set_center(chest1,248,357)
9.    circle:set_loop_delay(2000)
10.    circle:set_angle_speed(200)
11.    circle:set_duration(2000)
12.    circle:set_max_rotations(10)
13.    circle:set_initial_angle(360)
14.    circle:set_clockwise(true)
17.    circle:start(gerf)
18.
19. end
20.

The only time anything happens is when I do the following.

Code: Lua
1. local map = ...
2. local game = map:get_game()
3. local hero = map:get_hero()
4.
5. function map:on_started()
6.   local circle = sol.movement.create("circle")
8.    circle:start(gerf)
9.
10. end

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #6 on: April 02, 2017, 08:32:24 pm »
No idea of what is wrong. A workaround is to use a custom entity (I recommend to make it traversable) and code the movement. Idea of how to do it:

local x, y
local r, a, t -- radius, angle, time

and in a timer that increments the angle by t:

local a = a0 + t
x = x0 + r * cos(a)
y = y0 + r * sin(a)

The remaining details are a programming exercise for you

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### Re: How do I make a circle movement?
« Reply #7 on: April 02, 2017, 08:50:45 pm »
So is this a bug?

#### llamazing

• Jr. Member
• Posts: 66
##### Re: How do I make a circle movement?
« Reply #8 on: April 03, 2017, 03:00:57 pm »
I don't think that script is going to do what you want it to do.

Code: Lua
1. circle:set_center(chest1,248,357)

You are setting the center point of the circle 248 pixels horizontally and 357 pixels vertically away from the chest (i.e. 434 pixels away from the chest in total).

With a radius of 360 pixels, your NPC will not be circling the chest. My guess is the reason the script is not working is because the path you've given the NPC takes it beyond the borders of the map? Hard to say without actually seeing your map.

Have you tried just this?
Code: Lua
1. circle:set_center(chest1)

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #9 on: April 03, 2017, 06:56:13 pm »
I agree with llamazing

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### Re: How do I make a circle movement?
« Reply #10 on: April 03, 2017, 07:11:41 pm »
llamazing is correct. I did that and changed my radius to 20. Is it possible to have the sprite use all 4 facing directions when going around an entity or would it be better to loop with the path movement?

Code: Lua
1. local circle = sol.movement.create("circle")
2. circle:set_center(chest1)
3. circle:set_loop_delay(1000)
4. circle:set_angle_speed(100)
5. circle:set_duration(100)
6. circle:set_max_rotations(100)
7. circle:set_initial_angle(100)
8. circle:set_clockwise(true)
11. circle:set_ignore_obstacles(true)
12. circle:start(gerf)

#### Diarandor

• Hero Member
• Posts: 700
• Cats are cool! （ΦωΦ）
##### Re: How do I make a circle movement?
« Reply #11 on: April 03, 2017, 07:21:28 pm »
You can do it with both types of movements. Try to change the direction of the NPC if necessary, when some of these events are called: "entity:on_movement_changed(movement)" or "entity:on_position_changed(x, y, layer)".

#### zutokaza

• Full Member
• Posts: 139
• Active - Making stories can take the most time.
##### Re: How do I make a circle movement?
« Reply #12 on: April 03, 2017, 09:33:45 pm »
I tried the following, but it did nothing. I normally would do this for an enemy to use the 4 way directions.

Code: Lua
1. function gerf:on_movement_changed()
2.
3.   sprite:set_direction(movement:get_direction4())
4. end