主题:  算法的问题?

5D荣誉斑竹

职务:普通成员
等级:4
金币:10.0
发贴:1537
#12001/12/8 22:14:58
--------------------------------------------------------------------------------
property pSprite -- sprite reference
property pSpeed -- integer, speed of the rock
property pAngRad -- float, angle rock is moving (in radians)
property pRotateAmt -- float, the amount the rock rotates each prepareFrame

on new me, aMember
pSprite = sprite(me.spriteNum)
pSpeed = Random(5) -- a random speed
pAngRad = pi()*Random(360)/180 -- a random angle
pRotateAmt = 1.1-(Random(10)*.2) -- a random rotation amount
return me
end

on prepareFrame me
-- rotate the sprite
pSprite.rotation = pSprite.rotation + pRotateAmt
-- move the sprite
pSprite.loc = pSprite.loc + point(pSpeed*cos(pAngRad), \
pSpeed*sin(pAngRad))
-- make sure it's on the stage
me.CheckBounds((the stage).sourceRect.width, \
(the stage).sourceRect.height)
end

on CheckBounds me, h, v
-- Checks to see if the sprite is off the stage,
-- if it is, it is moved to the side opposite it went off.
if pSprite.locH > h then
pSprite.locH = pSprite.locH - h
else if pSprite.locH < 0 then
pSprite.locH = pSprite.locH + h
end if
if pSprite.locV > v then
pSprite.locV = pSprite.locV - v
else if pSprite.locV < 0 then
pSprite.locV = pSprite.locV + v
end if
end

这是一个比较常见的游戏里的算法,这个Behaviour脚本控制精灵移动,原来在学flash actionscript时在flashkit上见过,没想到现在学lingo又看到了。有一地方我想搞清楚它。就是
-- rotate the sprite
pSprite.rotation = pSprite.rotation + pRotateAmt
-- move the sprite
pSprite.loc = pSprite.loc + point(pSpeed*cos(pAngRad), \
pSpeed*sin(pAngRad))
这里关于三角函数的算法哪位高手能把它解释一下吗?我数学不好,有些想不过来。
谢谢。



5D荣誉斑竹

职务:普通成员
等级:4
金币:10.0
发贴:1537
#22001/12/8 22:18:16
顺便问一下,这个pSprite.rotate是按什么坐标来算的,是the stage的坐标,还是精灵中心点的x,y坐标?



D计划-混沌

职务:管理员
等级:6
金币:15.2
发贴:3528
#32001/12/11 2:18:35
pSprite.rotation = pSprite.rotation + pRotateAmt
没看例子,不知道具体干什么的,好像就是随机的自身旋转
pSprite.loc = pSprite.loc + point(pSpeed*cos(pAngRad), pSpeed*sin(pAngRad))
x=pSpeed*cos(pAngRad)
y=pSpeed*sin(pAngRad)
x*x+y*y=pSpeed*pSpeed

pSprite.rotate是按精灵中心点