Help with directional velocity acceleration.

You must understand the Game Editor concepts, before post here.

Help with directional velocity acceleration.

Postby FERdeBOER » Fri Mar 11, 2011 12:51 pm

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.
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Help with directional velocity acceleration.

Postby Camper1995 » Fri Mar 11, 2011 1:14 pm

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 162 times


Have a nice day,

Camper :P
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

Re: Help with directional velocity acceleration.

Postby Hblade » Fri Mar 11, 2011 3:14 pm

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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Help with directional velocity acceleration.

Postby Game A Gogo » Fri Mar 11, 2011 4:00 pm

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 :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Help with directional velocity acceleration.

Postby lcl » Fri Mar 11, 2011 6:16 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help with directional velocity acceleration.

Postby schnellboot » Fri Mar 11, 2011 6:23 pm

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
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Help with directional velocity acceleration.

Postby FERdeBOER » Fri Mar 11, 2011 7:43 pm

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)
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Help with directional velocity acceleration.

Postby Hblade » Sat Mar 12, 2011 2:41 am

Well, i'm glad you got it working and all :D
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Help with directional velocity acceleration.

Postby FERdeBOER » Sat Mar 12, 2011 10:13 am

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.
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Help with directional velocity acceleration.

Postby skydereign » Sat Mar 12, 2011 10:27 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with directional velocity acceleration.

Postby FERdeBOER » Sat Mar 12, 2011 10:49 am

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?
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Help with directional velocity acceleration.

Postby skydereign » Sat Mar 12, 2011 10:55 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with directional velocity acceleration.

Postby FERdeBOER » Sat Mar 12, 2011 11:35 am

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
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Help with directional velocity acceleration.

Postby skydereign » Sat Mar 12, 2011 11:45 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help with directional velocity acceleration.

Postby FERdeBOER » Sat Mar 12, 2011 12:43 pm

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.
Attachments
subgame.zip
(165.67 KiB) Downloaded 137 times
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Next

Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest