function lookto()

Learn how to make certain types of games and use gameEditor.

function lookto()

Postby feral » Fri Jul 18, 2008 2:15 am

a function for making actors look (or point) towards other actors or in certain directions
looksee.jpg
click to enlarge

If anyone has used the usual (commonly used) formula for making an actor "turn towards" or "look towards" another actor or to "point" the actor in a certain direction, they may not have notice it has a flaw.

the formula I am talking about, is the one most often used in asteroids type games, to point the ship in a certain direction
eg: is commonly used for animations which are made up of rotated sprites, in order to select the correct frame in the correct direction

usually something like this......
Code: Select all
ang=direction(x,y,player.x,player.y);
animpos=ang/360*nframes;


The use of this code makes thing very easy, but has three major flaws..

1/ It is always a half frame off :shock:
2/ your animation frames must start at a certain frame and be rotated clockwise for it to work
(the first frame should point right)
3/some odd numbered animations simply will not line up correctly
(also there is another problem with smaller numbered frames (2, 3, 4,8) which I will explain in a later post)

So, I have written a function that compensates for the half frame problem, the odd numbered frame problem, and allows you to start on any frame of the animation (so doesn't matter which direction the starting frame is pointed) AND, IF, the spritesheet/animation is rotated anticlockwise it will compensate for that.

attached is a demo of the frame problem I am talking about.. most of the time you will not notice it, as most animations usually have 24,32, or 36 or more frames, making the few degrees each frame is off almost unnoticeable,

but if you are making a RPG, or have a four way top down game... you will quickly see the problem

looktodemo1.zip
ged and data files demonstrates
the "half frame off" issue
(33.75 KiB) Downloaded 255 times


notice carefully how the actors on the right turn before you finish leaving their field of view... whereas the "compensated actors" follow you correctly. This can easily be compensated for by the programmer once they notice it, with a simple addition/subtraction of angles, if your game is easy !..

later I will explain how hard that compensation issue can get in more complex games.

I will post the rest of the explanation soon ( the starting angles problem,the anticlockwise problem, etc) ...how to use... etc soon .


feral.
Last edited by feral on Fri Jul 18, 2008 3:09 am, edited 3 times in total.
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: function lookto()

Postby feral » Fri Jul 18, 2008 3:02 am

OK, next problem...

the starting frame..

If you have a spritesheet, or series of sprites that are ready made for this type of thing in game editor you won't have a problem.

But, If you borrow sprites, or you rotated them without thinking of the start position then you are in trouble :?

the normal solution for the incorrect start position is to add an offset type routine to allow the animation to start on a different frame..

eg:
animpos = animpos+4; //offset by four frames
if (animpos>nframes) // if offset now over the number of frames
{
animpos=animpos-nframes
}

BUT, this only works if you actually have a frame that points to the right eg: a normal four frame animation or 8 or 16 etc

if you have an odd number of rotations, ( there may not be one pointed exactly to the right) or if the animation is NOT the usual "directly up/down directly left/right" type animation (for example think of isometric games which are rotated by 45 degrees diagonal), then in this situation, setting a frame offset will work, but you will find all your angles are off.

So instead of offsetting the number of frames... you are better off offsetting the 'angle' used to select a frame.

this means, for example, in a four way animation your first one might be one frame off pointing right... so you could just add 1 to each animpos.. OR 90 degrees to the angle used to select the correct frame.

however if you only have 3 frames and the first one points "up" you simply will not have a frame that points right, so instead of offsetting frames , you need to add 90 degrees to the angle used to select frames and all the math will still works.

lastly .... some times you will want the actor to look to "one side" or to turn around and face the other way, eg running away...

if you are using angles to select frames, and not the actual frame/animpos positions, then you can very quickly select any angle to face, and the math will still work.

hope this all makes sense so far, if not please ask..

attached is a sample file... you can see the different numbers of frames being used... and if you check out the spritesheets (in the data folder), you can see that most start "looking up" , and others start at different angles, and some do not even have a frame that looks right.

there is also some that go anticlockwise, yet using this function it all works out.
looksee.zip
data and ged files..
check out the spritesheets
(99.2 KiB) Downloaded 200 times


how the function works
first select an angle to look at, and save in a var..... in this example it always be the angle to the player... but, could be any angle for whatever reason.
Code: Select all
look=direction(x,y,player.x,player.y);

note you can use the built in angle var (enemy.angle), and in many cases you would as you would be looking in the angle you are moving in... but, sometimes you will want to look a different way, so use a local var..

then use the function
Code: Select all
animlook(look,90,1);

where
---- look=the angle to look to
---- 90= the offset "angle" of the starting sprite from direct right... or offset if you want to look somewhere else
-----1 or -1 , 1 equals clockwise animations, -1 = anticlockwise

note: currently ONLY works in the draw function of the actor being rotated.. i have tried to allow any actor to animate any other actor, but it is glitchy - hoping to fix that soon

feral..
ps: should all make sense so far, if not let me know..

next post will explain why i went to so much trouble with this... because most of you will be saying.. "so what ? it is just as easy to add offsets or angles or whatever when I make my own code".. well...one last thing coming :)
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: function lookto()

Postby feral » Fri Jul 18, 2008 7:29 am

forgot to add

in the last example---- looksee.zip

use the mouse to move the player around...(the smiley)
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest