Page 1 of 2

Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 12:51 pm
by FERdeBOER
Hi to all,
It's been long since the last time, but real life got me away from GE.

I'm in another project and having a problem with directional velocity:

I want the player to change its speed when clicking on a button, but slowly. For instance, when is stopped, I want it to speed up gradually to 5:

Code: Select all
if (player.directional_velocity < 5);
{
player.directional_velocity = player.directional_velocity * 1.2;
if (player.directional_velocity == 5);
{
player.directional_velocity = 5;
}
 


Te player actor accelerates, but instantly, from 0 to 5. Same happens form moving to halt. :?

I tried also using in draw actor directional_velocity = directional_velocity * Delta and the changing Delta values, but with the same results.

I'm sure I'm doing something wrong, but can't realize what... Any advice?

Thanks in advance.

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 1:14 pm
by Camper1995
Okay I don't know if I really understand what you need, but here is a demo. I hope that it's right you need.

(Press the red button to accelerate)
press directional velocity.ged
(2.7 KiB) Downloaded 161 times


Have a nice day,

Camper :P

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 3:14 pm
by Hblade
Use This
Code: Select all
if (player.directional_velocity < 5)
{
player.directional_velocity = player.directional_velocity * 1.2;
}
if (player.directional_velocity >= 5)
{
player.directional_velocity = 5;
}


You shouldn't have ; after the if statements. also you forgot to end your other if.

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 4:00 pm
by Game A Gogo
If "directional_velocity < 5" condition is true, how can "directional_velocity >= 5" be true in the same frame? I'd suggest adding an else here for good practice :)

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 6:16 pm
by lcl
Game A Gogo wrote:If "directional_velocity < 5" condition is true, how can "directional_velocity >= 5" be true in the same frame? I'd suggest adding an else here for good practice :)

I've found out that sometimes two if's don't work but if and else if do. So, why? I ask it from you because you seem to have the answer. :D

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 6:23 pm
by schnellboot
lcl wrote:
Game A Gogo wrote:If "directional_velocity < 5" condition is true, how can "directional_velocity >= 5" be true in the same frame? I'd suggest adding an else here for good practice :)

I've found out that sometimes two if's don't work but if and else if do. So, why? I ask it from you because you seem to have the answer. :D

there is no reason you just have to do so xD just kidding
I would like to know that too

Re: Help with directional velocity acceleration.

PostPosted: Fri Mar 11, 2011 7:43 pm
by FERdeBOER
Thank you very much for your answers!! :D

stupid me forgetting the ; it was so long since the last time I used GE that I forgot that, thanks. :oops:

so,finally I did a mix between your answers (based on Camper's code)

Code: Select all
if (playerspeed==1) // one third ordered
{
if (directional_velocity < 5)
{
    directional_velocity = directional_velocity*1.05; //accelerate
}
if(directional_velocity >5)
{
   directional_velocity = directional_velocity*0.98; //decelerate
}
}


With this I suppose that, when arriving at 5, the game oscillates between 4,99 and 5,01...

Now my player accelerates and decelerates slowly as I wanted it to :) (directional_velocity++ and -- was too fast for what I looked for)

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 2:41 am
by Hblade
Well, i'm glad you got it working and all :D

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 10:13 am
by FERdeBOER
Hblade wrote:Well, i'm glad you got it working and all :D


Don't be so confident, here I come with more questions :mrgreen:

Backwards!
Doing the same process above described, but with -5 as speed... well... my player doesn't move at all...

Code: Select all
 if(playerspeed=-1)
{
if(directional_velocity < -5) // if is going faster than -5, even positive speeds, maybe is wrong the logic, but I tried also the opposite.
{
directional_velocity = directional_velocity*0.98 // to decelerate
}
if(directional_velocity > -5) // if is going slower than -5, like -10.
{
directional_velocity = directional_velocity*1.03 // to accelerate
}
}


with some variations of this I have various results: dancing player; not moving at all player; player accelerating to infinite (wrong logic, I suppose).

I also doubt about facing angle... maybe I have to change also this?

And, abusing of your patience... is there a way to make angle changes gradually instead of sudden? So if my player is facing 0º and I order 90º, the player turns to the right gradually.

Thank you very much for your help and patience.

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 10:27 am
by skydereign
So you know, directional_velocity can never be negative. Whenever you set it to a negative number, it resets it to a positive value, and increases your angle by 180 degrees. So, the same logic you used for forwards should work for backwards.

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 10:49 am
by FERdeBOER
skydereign wrote:So you know, directional_velocity can never be negative. Whenever you set it to a negative number, it resets it to a positive value, and increases your angle by 180 degrees. So, the same logic you used for forwards should work for backwards.


I see... so I have to make my player to move in the opposite direction it faces... :? How?

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 10:55 am
by skydereign
I need to know more about your setup, but to take a stab in the dark... I assume you are using directional_velocity and angles due to rotational animations (if not then it should be quite simple). If you only have the rotational animations, then you just need to add 180 degrees to the animpos calculation, and modulate it %. You would only do this when a variable is triggered, telling the actor that it is going backwards. Setting it to 1 means you are moving backwards, and 0 means forwards.
Code: Select all
int newAngle = (int)(angle+backwardsVar*180)%360;
animpos=(newAngle)/nframes;


If this isn't what you were thinking about, do explain.

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 11:35 am
by FERdeBOER
Sorry, you're right, better if I explain it well from the start...
In the image you can see the player (the submarine) and around it a 360º ruler. When you click on one point of the ruler, the sub faces that point. Since here I have no problems. the GE icons at the right are the speed orders (will change that, but first I want to be sure they works). Using your help they work quite well. From top to down we have: backwards - stop - ahead 1/3 - ahead 2/3 - standard - full - flank. From stop to flank I have no problems now thanks to your help. Is the backwards order what I can't understand how to implement.
subtest.png
submarine game test

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 11:45 am
by skydereign
It would help if you also gave how you were handling the submarine. Assuming they are only active while the buttons on the right are pressed, all you need to do is set a variable on mouse button down.
actor -> mbd (left) -> Script Editor
Code: Select all
backwardsVar=1;
player.directional_velocity*=-1;


That will set backwardsVar to 1, and turn the player around (direction wise). Now, wherever you have your animation setting, you need to make sure it knows to adjust the animation when backwardsVar is 1. Like the example I gave you before. Normally if you flipped the angle, you would also flip the animation, but using the backwardsVar would fix that. If that isn't the problem you are having, do tell. Also, on mouse button up, you need to make sure to flip it back.
actor -> mbu (left) -> Script Editor
Code: Select all
backwardsVar=0;
player.directional_velocity*=-1;


This will manage the animations. The movement should be just like whatever you have been using before.

Re: Help with directional velocity acceleration.

PostPosted: Sat Mar 12, 2011 12:43 pm
by FERdeBOER
Thank you very much for your help... seems this is too much for me, or today I'm confused... or both... :oops:

I've attached the game. To show the angle ruler you have to click on the sub. To change speed, click on the GE icons on the right.
As is a "simulation", the sub doesn't stop unless you ordered it to (2nd button from the top). So once clicked in a speed button, the sub will maintain that speed.

The two problems I face are:
1) order reverse speed: when I order "back 1/3" (top GE icon), the sub should slow down to zero and then go backwards to -5, but always facing the original direction if I didn't order course change.

2) course change: I would like my sub to rotate slowly from its actual course to the one ordered. It's not a priority, thought.

Thank you very much for your help.