Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: BuzzMonkey99999 on Wed 29/07/2009 04:32:13

Title: 360 degree movement
Post by: BuzzMonkey99999 on Wed 29/07/2009 04:32:13
how do you make a person or car be able to spin 360 degrees(like gta1/2)
for a top down game?
Title: Re: 360 degree movement
Post by: R4L on Wed 29/07/2009 05:00:47
One way would to have all views. That means have a graphic for diagonals, and everything in a view. Then set the character to face the location of the mouse in repeatedly execute in the global script. That's a simple way.
Title: Re: 360 degree movement
Post by: Ryan Timothy B on Thu 30/07/2009 02:38:26
EDIT:  I forgot to explain that this is letting AGS rotate the image in whatever sprite slot.  This way you don't have to rotate by hand in a paint program.  Also allows for more angles.




These are the variables declared outside of a function.  I have mine declared in the Global Script because I want my script to work for multiple rooms.  If it's for only one room you can put it in the Room Script.


DynamicSprite* carimg[20];

struct car_struct{
 int y;     //the Y location of the car
 int x;     //the X location of the car
 int angle; //the angle of each car  
};

car_struct car[20];

I have 20 cars for my game, so therefor 20 in the array.




This is it's own function.  You can call it anytime by using:  rotate_cars(x);
Also put this in the Global Script if it will be accessed by multiple rooms.  Or just in the Room Script if it's only being used by one room.


function rotate_cars(int rc_number)
{
 //-----Assigns the UP graphic to the carimg array
 carimg[rc_number] = DynamicSprite.CreateFromExistingSprite(100, true);  
                 //takes the image from sprite slot 100, and preserves alpha channel.

 //-----AGS can't rotate an image if the number is lower than 1 or higher than 359
 int ri=car[rc_number].angle+90;   //add 90 to match the image direction with the Trig direction.
 if (ri>360) ri=ri-360;
 if (ri>0 && ri<360) carimg[rc_number].Rotate(ri);

 //-----Rotates the car and adjusts the Y position to center the car
 character[rc_number].y=(carimg[rc_number].Height/2)+car[rc_number].y; //adjust Y position to avoid offset verticle placement    

 //-----Assigns the Rotated Image to the Loop in VIEW1 (Each car has it's own Loop)
 ViewFrame *frame = Game.GetViewFrame(1, rc_number, 0); //gets the loop number  GetViewFrame(view, loop, frame)
 frame.Graphic = carimg[rc_number].Graphic; //assigns the graphic to the character loop
}





This is copied from the script for Road Racer 2 (modified slightly to dumb it down).  If it's still too confusing let me know.

I use characters for my cars and I assign a view to the character (before hand in the character editor).  Which I assigned view 1.




Also!  The struct in the variables allows you to do this, for example:
car[1].x=300;
or
car[1].angle=55;
etc.

It helps keep things more organized and easier to read and understand.

If Angle is 0, the car will point Right.  If Angle is 90, car points Down. etc.

Oh, and point your car Up in your sprite image (otherwise the angles would be off).  You want your car image to correspond with the trig.

Or you could delete the +90, on this part of the code:  
int ri=car[x].angle+90;
and have the car facing Right in the image.




Please let me know if it's too confusing.
Title: Re: 360 degree movement
Post by: BuzzMonkey99999 on Tue 04/08/2009 13:12:10
Haven't tested it yet so i don't know if i works but will test and get back to you
THanks
Title: Re: 360 degree movement
Post by: BuzzMonkey99999 on Wed 05/08/2009 09:25:08
it works( the game runs without error)
but how do i make it work
Title: Re: 360 degree movement
Post by: Ryan Timothy B on Wed 05/08/2009 23:29:02
I was going to explain how to use this code, but better yet, why don't I ask you one thing first.
How many cars do you plan on having active on the screen at once?  Will it be only 1 car or multiple cars?

If it's only 1 car, you'll have to modify my code without arrays and such so it's easier to manage and understand.  If it's multiple cars my code is already setup for it.


If it's only one car and you don't know how to go about modifying the code to better work it, let me know and I'll tinker with it for you.  And if it's multiple cars, the code has comments so just read through them and it'll explain how things work.

Title: Re: 360 degree movement
Post by: BuzzMonkey99999 on Thu 06/08/2009 04:08:25
Well i was going to test the code with one car but if the code is made for multiple cars then i will test it with multiple cars then.
In the real game I'm going to use multiple cars. It is GTA like game so all cars will be A.I controlled intill you enter one then you control it. If that helps!

Two questions:
So how do i call the function?
what would i have to change if it was just the one car?

Thanks
Title: Re: 360 degree movement
Post by: Khris on Thu 06/08/2009 13:02:57
I coded this module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=33122.0) a while ago, check it out if you like.
Title: Re: 360 degree movement
Post by: Ryan Timothy B on Thu 06/08/2009 23:10:29
You call the rotate function by this command:
rotate_cars(x);

Replace the X with your integer variable, and have a while statement since you plan on having multiple cars.

You'll also have to add a car sprite facing UP and change it's number to 100 (or just change the 100 in the script).

You'll also have to create a character for each car (it's better if each character for the cars is created all at once so that they are in numerical order--makes things easier in scripting).  You'll also have to individually assign a View to these characters.  I assigned the same View to all the characters (cars), just had a different Loop for each character (car).  Which you can see in the script I gave you, in this part:
  //-----Assigns the Rotated Image to the Loop in VIEW1 (Each car has it's own Loop)
  ViewFrame *frame = Game.GetViewFrame(1, rc_number, 0); //gets the loop number  GetViewFrame(view, loop, frame)
  frame.Graphic = carimg[rc_number].Graphic; //assigns the graphic to the character loop

This grabs the Frame from View 1, Loop (rc_number), and Frame 0.  Then places the rotated car image into it (carimg[rc_number]).

If you're having problems understanding this, I suggest you tinker around with it.

Anyway, this game you plan on making is pretty much exactly what I'm currently making as well.  So if you run into a brick wall, (and this is hoping I get mine done) I will share the source code so you can make your own.  But don't expect it anytime soon, or perhaps not even this year.
Title: Re: 360 degree movement
Post by: BuzzMonkey99999 on Fri 07/08/2009 09:35:40
Thanks for the script Ryan it has been a great help, it will be interesting to see your game when it is finished.