Page 1 of 1

Rotating Actors?

PostPosted: Wed May 26, 2010 3:02 am
by Vuredas
Hey guys, Im sort of a noob to GE (I joined about a day ago). So I was making a shooting game from top view and so far was doing fine but I came across a problem; How do you make an actor (my character from top view), follow the mouse cursor? Thnxs!

Re: Rotating Actors?

PostPosted: Wed May 26, 2010 8:14 am
by uan
+1 I've been wantin such a feature in GE too. I've used "draw to canvas" for such a feature, but it has seemed cumbersome for moderate-to-complex tests I've performed, I too would enjoy a "rotate sprite" feature in GE. fingers-crossed

Re: Rotating Actors?

PostPosted: Wed May 26, 2010 11:05 am
by Thanx
Draw Actor event:
player is the actor
Code: Select all
player.x = mousex;
player.y = mousey;

If you want it to smoothly move towards the mouse, I can offer looking up the MoveTo function...
:wink:

Re: Rotating Actors?

PostPosted: Wed May 26, 2010 12:28 pm
by Vuredas
Thanx wrote:Draw Actor event:
player is the actor
Code: Select all
player.x = mousex;
player.y = mousey;

I tried using the code and it made the character keep a distace from the mouse. Like it was parented to the mouse but from the bottom right corner. (Maybe I did something wrong?)
I only need the character to face the mouse.

Re: Rotating Actors?

PostPosted: Wed May 26, 2010 4:19 pm
by Thanx
Ok, to face the player towards the mouse is a bit tough, because you first have to create as many animations as as many angles you want the player to be able to face. Min. 8, but maybe even 12, 16 angles would be good.
Now there's a function called direction, which accepts 4 parameters, the coordinates of 2 points. Look it up in the game editor documentation (yes, I don't memorize code :) ).
Now that should return the angle of the imaginary line drawn between the 2 points, compared to an imaginary horizontal line pointing towards the right...
So you've got the angle calculated, you got the sprites: here's the coordination:
Again we'll use the Draw actor event, and use the script editor.
The code should look something like this:
int theanimnum;
theanimnum = //A formula;
ChangeAnimation(player, sprintf("angle%i", theanimnum), FORWARD);

This is if the animations are angle0, angle1, etc. The formula didn't come to my mind as I was writing this, but I think you'll be able to put it together, using the direction(), the number of angles you created sprites for, and the rotation angle between the sprites. It shouldn't be difficult...
On the other hand about making the player actually follow the mouse, the code I showed, and that you tried earlier isn't bad, but instead try and play around with adding, or subtracting from the mousex & mousey on the right side of the assignments, and eventually, after experimenting, you will get the right values to add/subtract....

Hope this was all understandable and clear. :wink:

PS: The reason for these complications is because GE can't really rotate sprites, no built in routines. Other users have coded some sprite rotational things, you may want to search the forums, for sprite rotation, and scaling. But I think these all use canvas actors, and maybe you don't want to deal with those...
Good luck! :)

Re: Rotating Actors?

PostPosted: Thu May 27, 2010 3:24 am
by Vuredas
Uhh... Okay... Could i have an example of it?

Re: Rotating Actors?

PostPosted: Thu May 27, 2010 7:39 am
by uan
If i understand the method described above, that would take a LOT more precious RAM from a portable platform to load in separate animations for every rotational angle an actor might need to represent.

So if you have a tile or tiles representing hundreds of frames of animation, and your character can be at 8 different rotational angles, thats 8x the tiles that need to be loaded.

Hopefully a more ram/disk-space sensitive solution arrives soon for GE. I'm impressed by the ease and power of GE so far. thanks Maks and Andreas

Re: Rotating Actors?

PostPosted: Thu May 27, 2010 6:37 pm
by Thanx
@uan
What I presented was the solution I am most familiar with. This doesn't mean it's the best. You can look up the method for doing everything with the canvas actor (I remember it's been discussed plenty of times on these forums), and you'll only need to use a few animations. But that will require greater knowledge of programming, and more CPU.
Choosing your methods is a matter of setting up priorities: What kinds of resources, quality factors are you willing to sacrifice? I don't develop for the mobile platforms, because I wouldn't be able to test them with anything. Therefore I usually take advantage of the RAM's size, to allow more CPU for even complicated routines... But that's just me, do it the way it suits YOU best!
On the other hand, I'm doing it this way here, because this is the simpler method. Also since Vuredas mentioned "mouse cursor" I think I'm free to assume that he's developing for PC also, which makes thee matter simpler. I actually rather would've let someone else answer, if the question were specifically mobile platform related...
Anyway, hope you understand me now... :)

@Vuredas: I can't create a quick demo right at this moment, but I promise I'll create one this weekend/saturday! Is that ok? :wink:

Re: Rotating Actors?

PostPosted: Tue Jun 01, 2010 11:44 pm
by Vuredas
sure thanks!

Re: Rotating Actors?

PostPosted: Mon Jun 07, 2010 12:33 am
by sillydraco
hey i got one working for you, its pretty simple! it uses one ScriptEditor set on DrawActor of your character, which checks if the mouse is left (<) or right (>) of x (which is the horizontal plane centered your character). then it checks if its above (<) or below (>) y (which is the vertical plane centered on your character). then it assigns the correct animation for your character.

check out the demo here:
http://www.scalesnfuzz.com/Shooter.zip

Code: Select all
if (x < xmouse - 30 && y < ymouse - 30)
{{ ChangeAnimation("Event Actor", "ld", FORWARD);}}


else
if (x < xmouse - 30 && y > ymouse + 30)
{ ChangeAnimation("Event Actor", "ul", FORWARD);}


else
if (x > xmouse + 30 && y > ymouse + 30)
{ ChangeAnimation("Event Actor", "ru", FORWARD);}


else
if (x > xmouse + 30 && y < ymouse - 30)
{ ChangeAnimation("Event Actor", "dr", FORWARD);}


else
if (xmouse < x && ymouse < (y + 30) && ymouse > (y - 30))
{ ChangeAnimation("Event Actor", "r", FORWARD);}


else
if (xmouse > x && ymouse < (y + 30) && ymouse> (y - 30))
{ ChangeAnimation("Event Actor", "l", FORWARD);}


else
if (xmouse < (x + 30) && xmouse > (x - 30) && ymouse > y)
{ ChangeAnimation("Event Actor", "d", FORWARD);}

else
if (xmouse < (x + 30) && xmouse > (x - 30) && ymouse < y)
{ ChangeAnimation("Event Actor", "u", FORWARD);}






and if you want it to move towards the mouse, you can add x/yvelocity scripts too! depending on which way your arrow is facing (see the animations of Player) you change the velocity by adding these scripts into the one above, just after the changeanimation command (make sure to move the } after this)

l
Code: Select all
xvelocity = -5;


r
Code: Select all
xvelocity = -5;


u
Code: Select all
yvelocity = -5;


d
Code: Select all
yvelocity = 5;


lu
Code: Select all
xvelocity = -5;
yvelocity = -5;


ru
Code: Select all
xvelocity = 5;
yvelocity = -5;


ld
Code: Select all
yvelocity = -5;
yvelocity = 5;


rd
Code: Select all
yvelocity = 5;
yvelocity = 5;

Re: Rotating Actors?

PostPosted: Mon Jun 07, 2010 5:54 am
by uan
@sillydraco

Great fleshed-out code. This too is the avenue I have had to explore for this very purpose in GE. Though, for a large sprite-sheet needed for every allowable angle, this method hogs up(for me) a bit too much of precious RAM that the portables have(IMO), it does get the job done.

And you're sharing of the code is much appreciated, as its very concise IMO. Thanks again.

Re: Rotating Actors?

PostPosted: Tue Jun 08, 2010 3:50 pm
by jimmynewguy
i dont have walking animations for any top down sprites, but if you loan me some for just a second i can make a way thats simple but easily missed. or i can just do one without walking animations and try to explain it for you...but either way i can make one :) (damn i talk really confusing...sorry)

EDIT: Here's a simple demo i found from a while back. Don't know if it's what you want or not. Just tell me what to change/fix. And maybe ignore that shotgun XD i looked at it and was like wow.....anyways, ya check it out!