Original GTA style driving

Started by blenderhead, Fri 17/06/2005 18:21:19

Previous topic - Next topic

blenderhead

Hi just wondering if it would be possible to have top down driving like in the original gta?

I know i could simply have a top down background with walkable areas on the road, have a car sprite and use the keyboard controls but i have tried this and its not quite right, i want it to feel like your controling the car being able to reverse and have a button to accelerate.

Its hard to explain but if you have played the original gta or gta 2 you will understand what i mean,

Thanks


DoorKnobHandle

#1
MINI-TUTORIAL

Introduction

This tutorial will make you control a car. It will be controlled with the cursor keys (UP to accelerate, LEFT to turn left and RIGHT to turn right).

Things that need to be done in AGS Editor

Start by adding a character to your game. Set it up how you want it, but make sure its script name is "CAR".

Let's get started with coding

First we'll add some variables that will store the cars velocity and the cars angle.

So add at the beginning of the global script file:

Code: ags

#define CAR_MAX_VELOCITY 3

int car_velocity;
int car_angle;


Now we'll need some constant variables, that will simply define a maximum car velocity for forward and backward gear.

Add underneath your first line:

Code: ags

#define CAR_MAX_VELOCITY_FORWARD 4
#define CAR_MAX_VELOCITY_BACKWARD 2


Now we scroll down to the 'repeatedly_execute ( )' function and handle the events when the player hits a certain key.

Add these lines to your 'repeatedly_execute ( )' function:

Code: ags

if ( IsKeyPressed ( 377 ) == 1 )
// right cursor key pressed
{
   // let the car turn to the right
   car_angle += 1;
}

if ( IsKeyPressed ( 375 ) == 1 )
// left cursor key pressed
{
   // let the car turn to the left
   car_angle -= 1;
}

if ( IsKeyPressed ( 372 ) == 1 )
// up cursor key pressed
{
   
   if ( car_velocity < CAR_MAX_VELOCITY )
	car_velocity++;
 
}
else
// up cursor key not pressed
{
   // make the car slow down unless it is already stopped
   if ( car_velocity > 0 )
      car_velocity--;
      
}

// now update the cars position
character[CAR].x = character[CAR].x + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Sin ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) );
character[CAR].y = character[CAR].y + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Cos ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) );

// make the car "look at" its angle value
cCar.FaceLocation ( character[CAR].x + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Sin ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) ), 
character[CAR].y + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Cos ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) ) );


The End

Compile it and everything should run fine. If not then tell me what is wrong, it is likely that I forgot something...

Theme

nice

but in gta the car can go like turn up and down
accelerate in every direction and stuff

o/

DoorKnobHandle

That is not possible in AGS unless a new version of AGS comes out where dynamic sprite rotation is supported!

Snarky

#4
Yes, it's possible with AGS. The car will just not always point in the exact direction you're driving. If you allow, say, 16 different visual orientations (N, NNW, NW, WNW, W, etc.), the difference should be small enough to be acceptable. For that, you only need 4 sprites. Since you can easily create them all from one base image by rotation, this should be easy enough. (You could even create a Photoshop macro to make them all for you.)

You only need two variables: one for your speed and one for your orientation (in radians or degrees), which is the direction you're going. Then let the up/down arrows control your speed and the left/right arrows control your orientation. Trigonometry is required to calculate your movement, but that's easy enough.

Gregjazz

Quote from: Exsecratus on Fri 17/06/2005 23:03:15
nice

but in gta the car can go like turn up and down
accelerate in every direction and stuff

o/

I made something very similar. It was an airplane with 30 angles of rotation (rendered in 3D, of course) and the controls were just like GTA. You had inertia and acceleration in all the angles and all the good stuff. So it is definitely possible. :)

The thing is, you just have to render or get seperate pictures of the car/plane in all the needed directions, AGS won't rotate it for you.

DoorKnobHandle

Sorry, for that. It will look a little wierd if the car is facing in only 8 possible directions and is driving in 360 degrees but it is possible, that's true.

I'll update the tutorial. Check it out and see what changed!

Gregjazz

Quote from: [ ... ] on Sat 18/06/2005 11:51:48
Sorry, for that. It will look a little wierd if the car is facing in only 8 possible directions and is driving in 360 degrees but it is possible, that's true.

I'll update the tutorial. Check it out and see what changed!

Yeah, definitely stay away from the in-programmed AGS character limitations such as only having a maximum of 8 angles.

blenderhead

#8
Great!

Thanks for the help, ill get started on the tutorial right away! :)

Edit- ok ive cut and pasted the tutorial in to the global script but im getting an error saying:

error (line 50) undefined symbol 'CAR'

ive have named my character CAR so i dont see what the problem is though im not to experienced in scripting, perhaps ive pasted it in the wrong place?

strazer

Are you sure your character's "Script name" is CAR, not just his "Full name"?

blenderhead

Ahh thats what was wrong but now another problem
saying

Error (line 20): prase error: unexpected if

im not sure in pasting in to the right places here is my script-

// main global script file
#define CAR_MAX_VELOCITY 3

int car_velocity;
int car_angle;
#define CAR_MAX_VELOCITY_FORWARD 4
#define CAR_MAX_VELOCITY_BACKWARD 2


#sectionstart game_startÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
Ã,  // called when the game starts, before the first room is loaded
}
#sectionend game_startÃ,  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart repeatedly_executeÃ,  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
Ã,  // put anything you want to happen every game cycle here
}if ( IsKeyPressed ( 377 ) == 1 )
// right cursor key pressed
{
Ã,  Ã, // let the car turn to the right
Ã,  Ã, car_angle += 1;
}

if ( IsKeyPressed ( 375 ) == 1 )
// left cursor key pressed
{
Ã,  Ã, // let the car turn to the left
Ã,  Ã, car_angle -= 1;
}

if ( IsKeyPressed ( 372 ) == 1 )
// up cursor key pressed
{
Ã,  Ã, 
Ã,  Ã, if ( car_velocity < CAR_MAX_VELOCITY )
car_velocity++;

}
else
// up cursor key not pressed
{
Ã,  Ã, // make the car slow down unless it is already stopped
Ã,  Ã, if ( car_velocity > 0 )
Ã,  Ã,  Ã,  car_velocity--;
Ã,  Ã,  Ã, 
}

// now update the cars position
character[CAR].x = character[CAR].x + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Sin ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) );
character[CAR].y = character[CAR].y + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Cos ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) );

// make the car "look at" its angle value
cCar.FaceLocation ( character[CAR].x + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Sin ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) ),
character[CAR].y + FloatToInt ( IntToFloat ( car_velocity ) * Maths.Cos ( Maths.DegreesToRadians ( IntToFloat ( car_angle ) ) ) ) );

#sectionend repeatedly_executeÃ,  // DO NOT EDIT OR REMOVE THIS LINE

strazer

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here
}if ( IsKeyPressed ( 377 ) == 1 )
// right cursor key pressed

Remove that brace and put it at the end of the rep_ex function:

}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE

blenderhead

ok done that but now there's another

error (line 50): undefined symbol 'floatToInt'

Ashen

Commands are case sensitive - it should be 'FloatToInt' not 'floatToInt', which is why you'd get the error. None of them seem to be wrong in the script you posted (and I'm guessing it's a direct copy-paste of your global script?), but check anyway (might be easier if you use Ctrl-F to search for floatToInt).
I know what you're thinking ... Don't think that.

blenderhead

nope it a capital F

any other ideas :(??

Ashen

What version are you using?

I think most of the maths functions (and FloatToInt) are 2.7 commands, so if you're using 2.62, it's not going to recognise them.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk