Re: Help with "facing" routine
- Posted by Jiri Babor <jbabor at PARADISE.NET.NZ> Jul 01, 1998
- 695 views
One or two more symmetries could be explored to further reduce the code size, but I doubt it would result in any speed increase. Enjoy. Jiri function determine_facing(sequence char_pos, sequence point) integer dx,dy,i atom raw_t,t dx=point[1]-char_pos[1] dy=point[2]-char_pos[2] if dy then -- if dy!=0 raw_t=-dx/dy if raw_t>0 then t=raw_t else t=-raw_t end if -- 1st quadrant if t<0.1317 then i=1 -- tan( 7.5 deg) elsif t<0.4142 then i=2 -- tan(22.5 deg) elsif t<0.7673 then i=3 -- tan(37.5 deg) elsif t<1.3032 then i=4 -- tan(52.5 deg) elsif t<2.4142 then i=5 -- tan(67.5 deg) elsif t<7.5958 then i=6 -- tan(82.5 deg) else i=7 end if if raw_t<0 then -- 2nd or 4th quadrant if dy>0 then i=14-i -- 2nd quadrant else -- 4th quadrant i=26-i if i=25 then i=1 end if end if elsif dy>0 then i=12+i -- 3rd quadrant end if elsif dx>0 then i=7 -- horizontal, facing right else i=19 -- horizontal, facing left (or undetermined) end if return i end function -----Original Message----- From: Einar Mogen <nord.staernes at ROLLAG.MAIL.TELIA.COM> To: EUPHORIA at cwisserver1.mcs.muohio.edu <EUPHORIA at cwisserver1.mcs.muohio.edu> Date: Wednesday, 1 July 1998 10:45 Subject: Help with "facing" routine >I would like some help on a function that should work like this: > >I pass it two sequences, the first one containing the coordinates of the >active character, the second one containg the coordinates of the point that >the active character should look at. I have a total of 24 facings, the first >one being directly up, the second 15 degrees to the right, the second 30 >degrees to the right, and so on. They are indexed 1-24. The function is >supposed to return the facing index that most closely matches the character >looking towards the point. If anyone out there which is more stable at >mathematics like this could help me with the routine (it should be as fast >as possible, since it will be called many times each frame of the game) then >I would be very grateful. I've started working on it, but now I'm both lazy >and tired, and I'm not too keen on this kind of mathematics. Following is an >outline of the function: > >function determine_facing(sequence char_pos, sequence point) > -- find the closest match of the 24 possibilites > return facing -- 1 to 24 >end function > >Any help is greatly appreciated, the main thing I'm out for is the fastest >way to find the angle which the point is at, relative to the character, >either in degrees (or radians) or even better expressed with the indexes. > >Einar (who hopes his english is not _totally_ hopeless) >