Falling speed

J

James7285

Guest
Hello,

Another question relating to my rocket game and the speed.

When you press up on the keyboard the rocket flies upwards and the speed of the rocket is displayed in the top left corner. When you let go of the up arrow or run out of fuel gravity kicks in, the rocket loses speed and begins to fall back to earth.

The problem I'm having is that as the rocket falls the speed drops to 0, but then starts going back up again as obviously the rocket is picking up speed going downwards.

I want it so that if the rocket is falling and the speed hits 0, it stays on 0. Then if the thrust kicks back in obviously it will rise back up again. At the moment to display the speed I am using the following:

mph = round(obj_rocket.speed * 20)
draw_text(x, y + 30, "Speed: " + string(mph) + "mph ")

Thanks for the help.

James
 

wamingo

Member
it sounds a bit weird and I'm probably interpreting you wrong, but maybe you want the max() function?
Code:
max( round( speed*20 ), 0 )  // returns 0 or above
 
J

James7285

Guest
Thanks for the reply! It's more likely me explaining it wrong! lol

So basically the speed text at the top left of the screen always shows the rockets speed. That's fine for when it's thrusting upwards. However once you let go of the up arrow or run out of fuel and start falling down.... the speed drops to 0 as your acceleration comes to a halt, but then starts going up again as it is basically now showing your falling speed.

I want it to only show your speed as you're thrusting upwards. If you start dropping and the speed hits 0, I want it to stay at 0 unless you start accelerating upwards again.

Thanks once again for the help so far.

James
 

wamingo

Member
To be clear, you don't want to change the rocket's behavior, just the text display, correct?

And max() is not what you want, why?
 
A

Aura

Guest
Initialize a variable which would tell whether the rocket is going up or down.

Code:
if (y >= yprevious) {
    going_up = 0; // Basically "false" is the same as 0 in GML
}
else if (y < yprevious) {
    going_up = 1;
}
Then you can do this:

Code:
mph = round(obj_rocket.speed * 20) * obj_rocket.going_up;
But an accurate answer depends on the method you are using for movement. For instance, that might not work if the rocket is static and everything else dynamic, so if that does not help, please share information in that regard. ^^
 
J

James7285

Guest
To be clear, you don't want to change the rocket's behavior, just the text display, correct?

And max() is not what you want, why?
That's correct, just the text display. I don't think max would work as when the rocket is falling, the number doesn't go into a minus number, it just shoots back up. So the program needs to also know the direction the ship is going.... unless I'm missing something?


Initialize a variable which would tell whether the rocket is going up or down.

Code:
if (y >= yprevious) {
    going_up = 0; // Basically "false" is the same as 0 in GML
}
else if (y < yprevious) {
    going_up = 1;
}
Then you can do this:

Code:
mph = round(obj_rocket.speed * 20) * obj_rocket.going_up;
But an accurate answer depends on the method you are using for movement. For instance, that might not work if the rocket is static and everything else dynamic, so if that does not help, please share information in that regard. ^^
Thanks for the post, I think what you have put there is very much along the lines of what I'm looking for. However when I use that I get the following error on startup:


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_hud:

Variable obj_rocket.<unknown variable>(100003, -2147483648) not set before reading it.
at gml_Object_obj_hud_DrawEvent_1 (line 23) - mph = round(obj_rocket.speed * 20) * obj_rocket.going_up;
############################################################################################

Thanks

James
 
A

Aura

Guest
Where are you using the code that I gave you earlier? That needs to be in the Step event of the rocket object. Also make sure that you are not declaring it as a local variable (with the help of the var keyword) and it is declared as an instance variable in the Create event of the rocket object:

Code:
going_up = 0;
 
J

James7285

Guest
Sorry, that was my bad. I have an object called obj_hud which is where I have my draw event for things like speed and fuel. I put everything in there.

I have now put the the first bit of code in the step event of the obj_rocket and the second part in the draw event of my obj_hud.

Now when I fly the speed just stays at 0mph the whole time.
 

wamingo

Member
I'm a bit fuzzy on exactly how xprevious/yprevious works, but my experience they become useless when you use the builtin speed. I had loads of trouble with them a while back.

So you can set your own xprev=x and yprev=y at the end of your step or end_step.
Or since you're setting the built in speed and direction, you could try this:
Code:
if (direction<180)   //should be automatically kept between 0-360
    going_up=1
else
    going_up=0
and if you don't feel sideways is OK, then
direction>0 && direction<180
 
J

James7285

Guest
Brilliant, that works fine!

Thank you both very much for your help! It's been very useful
 
S

Snail Man

Guest
PS: don't forget to change the topic prefix to 'solved'!
 
Top