Shaiya - WayPoint System
Posted: Thu Oct 09, 2008 11:21 pm
I'm working on a waypoint system for shaiya, the functions are pretty simple rotate_to(degrees) and move_to(pos) functions (pos is table w/x,y,z values).
Here is a extract of some of the code. You won't b able to compile, but is pretty easy to read.
Everything works except one function, I need to work out if the rotation should occur clockwise (CW), or counter-clockwise (CCW), depending on which will be closest.
I'm lost on how to do it, here is the prototype:
Anyone feel like filling in the solution for the function?
This thread had a issue just like this, though I'm not sure which code I should port.
http://board.flashkit.com/board/showthread.php?t=766397
Here is a extract of some of the code. You won't b able to compile, but is pretty easy to read.
Code: Select all
function Avatar:move_to(pos)
local l_check_interval = 250
local l_pos_cur = self:get_position()
debug_message("pos (" .. math.floor(l_pos_cur.x) .. "," .. math.floor(l_pos_cur.z) .. ")")
debug_message("move_to (" .. math.floor(pos.x) .. "," .. math.floor(pos.z) .. ")")
local l_pos_relative = {
x=(pos.x - l_pos_cur.x),
y=(pos.z - l_pos_cur.z),
}
local l_deg = get_quadrantal_angle(l_pos_relative)
self:rotate_to(l_deg)
local l_tolerance = 5
local l_pos_new = {
min_x=(pos.x - l_tolerance),
max_x=(pos.x + l_tolerance),
min_z=(pos.z - l_tolerance),
max_z=(pos.z + l_tolerance),
}
while (true) do
l_pos_cur = self:get_position()
--debug_message("cur_pos (" .. math.floor(l_pos_cur.x) .. "," .. math.floor(l_pos_cur.z) .. ")")
if (l_pos_cur.x >= l_pos_new.min_x and
l_pos_cur.x <= l_pos_new.max_x and
l_pos_cur.z >= l_pos_new.min_z and
l_pos_cur.z <= l_pos_new.max_z)then
break;
end
self:move(DIRECTION_UP, 250)
coroutine.yield()
end
debug_message("moved_to (" .. math.floor(l_pos_cur.x) .. "," .. math.floor(l_pos_cur.z) .. ")")
end
function Avatar:rotate_to(degrees)
local l_pos_cur = self:get_rotation(l_pos)
local l_deg_cur = get_quadrantal_angle(l_pos_cur)
debug_message("deg_cur: " .. l_deg_cur)
debug_message("deg_new: " .. degrees)
local l_increment = 1
local l_pos_new = {
x=math.cos(math.rad(degrees)),
y=math.sin(math.rad(degrees)),
}
--debug_message("l_x: " .. l_pos_new.x .. ", " .. "l_y: " .. l_pos_new.y)
local l_tolerance = (l_increment / 90)
local l_direction = get_rotate_direction(l_deg_cur, degrees)
l_pos_new.min_x = l_pos_new.x - l_tolerance
l_pos_new.max_x = l_pos_new.x + l_tolerance
l_pos_new.min_y = l_pos_new.y - l_tolerance
l_pos_new.max_y = l_pos_new.y + l_tolerance
while (true) do
self:spin(l_direction, l_increment)
l_pos_cur = self:get_rotation(l_pos)
--debug_message("l_cx: " .. l_pos_cur.x .. ", " .. "l_cy: " .. l_pos_cur.y)
if (l_pos_cur.x >= l_pos_new.min_x and
l_pos_cur.x <= l_pos_new.max_x and
l_pos_cur.y >= l_pos_new.min_y and
l_pos_cur.y <= l_pos_new.max_y)then
break;
end
coroutine.yield()
end
--debug_message("l_cx: " .. l_pos_cur.x .. ", " .. "l_cy: " .. l_pos_cur.y)
--Avatar:move(DIRECTION_UP, 500)
l_deg_cur = get_quadrantal_angle(l_pos_cur)
debug_message("l_deg_cur: " .. l_deg_cur)
end
function get_quadrantal_angle(pos)
-- find out the opposite angle of the right sided triangle.
local l_deg_cur = 90 - (math.deg(math.atan(pos.x / pos.y)))
debug_message("l_deg_cur: " .. l_deg_cur)
-- If it's in the 3rd or 4th Quadrant then add 180 degrees.
if (get_quadrant(pos) >= 3) then
l_deg_cur = l_deg_cur + 180
end
return round(l_deg_cur, 0) % 360
end
function get_quadrant(pos)
if (pos.x >= 0) then
if (pos.y >= 0) then
return 1
else
return 4
end
else
if (pos.y >= 0) then
return 2
else
return 3
end
end
end
I'm lost on how to do it, here is the prototype:
Code: Select all
--[[
Get the closest rotation direction.
@param degree The current degrees that object is facing (0-360).
@param degree_new The new degree that object wants to face (0-360).
@return Rotation direction, DIRECTION_RIGHT for ClockWise (CW), DIRECTION_LEFT for CCW.
]]--
function get_rotate_direction(degree, degree_new)
-- if should rotate left
-- return DIRECTION_LEFT
-- else
-- return DIRECTION_RIGHT
end
This thread had a issue just like this, though I'm not sure which code I should port.
http://board.flashkit.com/board/showthread.php?t=766397