Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, 1 invisible), 18,758 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
movement script error #104190
12/27/06 23:21
12/27/06 23:21
Joined: Dec 2006
Posts: 18
S
soraga Offline OP
Newbie
soraga  Offline OP
Newbie
S

Joined: Dec 2006
Posts: 18
Ive been working on this all day, Im stumped! For some reason whenever I start the engine, my model, a car, starts to move backwards very slowly. Everything else works as normal. I cant figure out the reason for this strange backwards movement, any help would be appreciated.

Code:


var video_mode = 7; // 800x600 pixels
var video_depth = 32; // 32 bit mode

////////////////////////////////////////////////////////////////////////////////////

string homework18_wmb = <homework18.wmb>;

ENTITY* car;

bmap bmpSpeed = "speedometer.bmp";

////////////////////////////////////////////////////////////////////////////////////

var accel = 1; //acceleration
var reverse = -1; //reverse acceleration
var absSpeed = 1; //universal speed, to be displayed by the speedometer negative numbers are reverse for now

function main()
{
fps_max = 30;
level_load (homework18_wmb);
wait (3);
while(1) //set up the camera
{
camera.x = car.x;
camera.y = car.y - 300;
camera.z = car.z + 1000;
camera.pan = 90; // set the correct orientation for the camera
camera.tilt = -68; // using its pan and tilt angles

wait(1);
}
}

panel speedometer
{
pos_x = 0;
pos_y = 0;
layer = 1;
bmap = bmpSpeed;
digits(35,45,3,_a4font,1,absSpeed);
flags = overlay, visible;
}

action my_car
{
car = me;
my.fat = on;
my.narrow = on;
c_setminmax(my);
while (1)
{
// move the car using relative_speed
if (key_a == on) //pans the car, but only if moving
{
if(accel != 0)
{
my.pan += 5; // increase the pan angle of the car
}
if(reverse != 0)
{
my.pan += 5; // decrease the pan angle of the car
}
}

if (key_d == on) //pans the car, but only if moving
{
if(accel != 0)
{
my.pan -= 5; // decrease the pan angle of the car
}
if(reverse != 0)
{
my.pan -= 5; // decrease the pan angle of the car
}
}

if (key_w == on) //while pressing w...
{
accel += 1; //accelerate by increasing the x value...
c_move (my, vector(accel,0, 0), nullvector, glide); //in this vector
if(accel >= 40) //car cannot exceed 40 speed
{
accel = 40;
}
if(accel >= 1) //set up the value for the speedometer to display
{
absSpeed = accel; //speedometer value == accel
}
}

if(key_w == off) //if you let off on the gas...
{
accel -= .5; //decelerate
c_move(my,vector(accel,0,0),nullvector,glide);
if(accel <= 1) //stop decelerating when not moving
{
accel = 0;
}
if(accel >= 1) //set up values for speedometer
{
absSpeed = accel;
}
}

if(key_s == on) //when hitting reverse
{
c_move(my,vector(reverse,0,0), nullvector, glide); //decrease x value
reverse -= 1; //in this vector to back up
if(reverse < -18)
{
reverse = -18;
}
if(accel <= 1) //set up values for speedometer
{
absSpeed = reverse;
}
}

if(key_s == off) //when you let off on the gas
{
c_move(my,vector(reverse,0,0),nullvector,glide); //slow down
reverse += 1;
if(reverse >= -1)
{
reverse = 0;
}
if(accel <= 1) //set up values for speedometer
{
absSpeed = reverse;
}
}

if(key_space == on) //brakes
{
if(accel > 0)
{
accel -= 2.5;
}
if(reverse < 0)
{
reverse += 2.5;
}
}


wait (1);

}
}




This is some modified and reworked code from the c-script tutorial, Im new to Gamestudio. Im using Gamestudio trial version 6.40.5. Thanks

Re: movement script error [Re: soraga] #104191
12/28/06 01:06
12/28/06 01:06
Joined: Feb 2002
Posts: 357
Florida
Zelek Offline
Senior Member
Zelek  Offline
Senior Member

Joined: Feb 2002
Posts: 357
Florida
I haven't done that tutorial, but from looking at your car action, you have two places that look like the problem:

Code:

if(key_w == off)
{
...
...
}

if(key_s == off)
{
...
...
}



These are both within a while loop that is constantly checking to see if keys are pressed. But you have these two set up so that if keys AREN'T pressed, then they do something. So when you start up your level, they instantly go into effect.

Your problem is that you're trying to make these things happen when the user "lets off" the gas, but the code executes whenever the key is not being pressed. You need to make these things happen after the key has already been pressed, and THEN the user releases. Give it a shot for a little while and if you can't figure it out, I'll help.

Re: movement script error [Re: Zelek] #104192
12/28/06 03:27
12/28/06 03:27
Joined: Dec 2006
Posts: 18
S
soraga Offline OP
Newbie
soraga  Offline OP
Newbie
S

Joined: Dec 2006
Posts: 18
I see! Your explanation makes perfect sense, however...I cant seem to figure out how to apply it. I tried replacing (key_s == off) and (key_w == off) with (key_lastpressed == 31) and (key_lastpressed == 17), but this didnt work too well. Are there any other functions that can do the job better?

Thanks again

Re: movement script error [Re: soraga] #104193
12/28/06 04:03
12/28/06 04:03
Joined: Feb 2002
Posts: 357
Florida
Zelek Offline
Senior Member
Zelek  Offline
Senior Member

Joined: Feb 2002
Posts: 357
Florida
Hmm, after a second look, there might be an easier fix. Change:

Code:

if(key_w == off) //if you let off on the gas...
{
accel -= .5; //decelerate
c_move(my,vector(accel,0,0),nullvector,glide);
if(accel <= 1) //stop decelerating when not moving
{
accel = 0;
}
if(accel >= 1) //set up values for speedometer
{
absSpeed = accel;
}
}



to this:

Code:

if(key_w == off) //if you let off on the gas...
{
accel -= .5; //decelerate
if(accel <= 1) //stop decelerating when not moving
{
accel = 0;
}
if(accel >= 1) //set up values for speedometer
{
absSpeed = accel;
}
c_move(my,vector(accel,0,0),nullvector,glide);

}



See what changed? This way, the updated acceleration isn't applied until after the check to make sure it doesn't go negative. That should keep your car from going backwards at the start.

There are other ways to approach it too. You could do something like:

Code:

if(key_w == off && accel > 0)
{
...
}



So that it's only executed if the car is already accelerating forward when key_w is off.

Your code for slowing down the reverse speed when the user lets up the key might be a little awkward since you're switching between the accel and reverse variable.

Re: movement script error [Re: Zelek] #104194
12/28/06 04:22
12/28/06 04:22
Joined: Dec 2006
Posts: 18
S
soraga Offline OP
Newbie
soraga  Offline OP
Newbie
S

Joined: Dec 2006
Posts: 18
It worked! But now I have another problem. When I start the engine, my speedometer correctly reads a speed of zero. But after moving the car and slowing back down to a complete stop, the speedometer reads 1. Once again, Ive got no idea whats causing this.

Thanks for your help, the fix was brilliant!

Re: movement script error [Re: soraga] #104195
12/28/06 04:24
12/28/06 04:24
Joined: Dec 2006
Posts: 18
S
soraga Offline OP
Newbie
soraga  Offline OP
Newbie
S

Joined: Dec 2006
Posts: 18
I forgot...heres the updated code:

Code:


var video_mode = 7; // 800x600 pixels
var video_depth = 32; // 32 bit mode

////////////////////////////////////////////////////////////////////////////////////

string homework18_wmb = <homework18.wmb>;

ENTITY* car;

bmap bmpSpeed = "speedometer.bmp";

////////////////////////////////////////////////////////////////////////////////////

var accel; //acceleration
var reverse; //reverse acceleration
var absSpeed; //universal speed, to be displayed by the speedometer negative numbers are reverse for now

function main()
{
fps_max = 30;
level_load (homework18_wmb);
wait (3);
while(1) //set up the camera
{
camera.x = car.x;
camera.y = car.y - 300;
camera.z = car.z + 1000;
camera.pan = 90; // set the correct orientation for the camera
camera.tilt = -68; // using its pan and tilt angles

wait(1);
}
}

panel speedometer
{
pos_x = 0;
pos_y = 0;
layer = 1;
bmap = bmpSpeed;
digits(35,45,3,_a4font,1,absSpeed);
flags = overlay, visible;
}

action my_car
{
car = me;
my.fat = on; // the first 3 lines of code
my.narrow = on; // enable the new
c_setminmax(my); // collision detection system used by A6.2 or newer versions
while (1)
{
// move the car using relative_speed
if (key_a == on) //pans the car, but only if moving
{
if(accel != 0)
{
my.pan += 5; // increase the pan angle of the car
}
if(reverse != 0)
{
my.pan += 5; // decrease the pan angle of the car
}
}

if (key_d == on) //pans the car, but only if moving
{
if(accel != 0)
{
my.pan -= 5; // decrease the pan angle of the car
}
if(reverse != 0)
{
my.pan -= 5; // decrease the pan angle of the car
}
}

if(key_w == on) //while pressing w...
{
accel += 1; //accelerate by increasing the x value...

if(accel >= 40) //car cannot exceed 40 speed
{
accel = 40;
}
if(accel >= 0) //set up the value for the speedometer to display
{
absSpeed = accel; //speedometer value == accel
}
c_move (my, vector(accel,0, 0), nullvector, glide); //in this vector
}

if(key_w == off && accel > 0) //if you let off on the gas...
{
accel -= .5; //decelerate
if(accel > 0)
{
absSpeed = accel;
}
if(accel <= 1) //make sure you cannot go backwards when accelerating
{
accel = 0;
}
c_move(my,vector(accel,0,0),nullvector,glide);
}

if(key_s == on) //when hitting reverse
{
c_move(my,vector(reverse,0,0), nullvector, glide); //decrease x value
reverse -= 1; //in this vector to back up
if(reverse < -18)
{
reverse = -18;
}
if(accel <= 1) //set up values for speedometer
{
absSpeed = reverse;
}

}

if(key_s == off && reverse < 0) //when you let off on the gas
{
c_move(my,vector(reverse,0,0),nullvector,glide); //slow down
reverse += 1;
if(reverse >= -1)
{
reverse = 0;
}
if(accel <= 1) //set up values for speedometer
{
absSpeed = reverse;
}
}

if(key_space == on)
{
if(accel > 0)
{
accel -= 2.5;
}
if(reverse < 0)
{
reverse += 2.5;
}
}


wait (1);

}
}




Re: movement script error [Re: soraga] #104196
12/28/06 05:46
12/28/06 05:46
Joined: Feb 2002
Posts: 357
Florida
Zelek Offline
Senior Member
Zelek  Offline
Senior Member

Joined: Feb 2002
Posts: 357
Florida
Well, just try to imagine what's going on in your head, step by step. You have a panel called speedometer, that displays your speed via a variable called absSpeed. So we know that absSpeed must not be getting set correctly.

We also know that absSpeed is incorrect when the car slows down to a stop, so we should probably look at our code for when we release the w key.

Code:

if(key_w == off && accel > 0)
{
accel -= .5;
if(accel > 0)
{
absSpeed = accel;
}
if(accel <= 1)
{
accel = 0;
}
c_move(my,vector(accel,0,0),nullvector,glide);
}



Now work through this part step by step, that's the same way the computer reads it. First, it checks if accel is above 0, then sets the absSpeed variable to whatever accel is. But right after that, it sets accel to 0 whenever it's less than or equal to 1. You should update the absSpeed variable after accel is set to 0.

Re: movement script error [Re: Zelek] #104197
12/28/06 23:49
12/28/06 23:49
Joined: Dec 2006
Posts: 18
S
soraga Offline OP
Newbie
soraga  Offline OP
Newbie
S

Joined: Dec 2006
Posts: 18
My friend, you are a genius! Here's how I fixed it:

Code:

if(key_w == off && accel > 0) //if you let off on the gas...
{
accel -= .5; //decelerate
if(accel <= 1) //make sure you cannot go backwards when accelerating
{
accel = 0;
absSpeed = accel;
}
if(accel > 0)
{
absSpeed = accel;
}
c_move(my,vector(accel,0,0),nullvector,glide);
}



thank you very much

Re: movement script error [Re: soraga] #104198
12/29/06 08:32
12/29/06 08:32
Joined: Dec 2005
Posts: 252
MyOwnKingdom
nipx Offline
Member
nipx  Offline
Member

Joined: Dec 2005
Posts: 252
MyOwnKingdom
Multiply your movement vectors with time or time_step


Code:
c_move(my,vector(accel.x * time,0,0),nullvector,glide);  



or

Code:
c_move(my,vector(accel.x * time_step,0,0),nullvector,glide);





you dont really need to set absSpeed twice:

Code:

absSpeed = accel;
}
if(accel > 0)
{
absSpeed = accel;
}



just:

Code:

if(accel <= 1)
{
accel=0;
}

absSpeed=accel;




This should work to but correct me when im wrong



nipx


Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1