Author Topic: Velocity  (Read 9363 times)

0 Members and 1 Guest are viewing this topic.

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Velocity
« on: December 07, 2011, 07:36:25 am »
Ok, in the Tag game ive seen that when you move the character, he moves normally then slows down and stops when you stop pressing the move button. How would I achieve velocity similar to this to make some of my programs a little more realistic?
Be it may, a ball bouncing up and down, TAG movement, jumping slows as I get higher and then I fall, things like that!
Thanks!

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Velocity
« Reply #1 on: December 07, 2011, 07:44:39 am »
you can store velocity to some variable, and make it increase when getKey is true, and decrease to 0 when getKey is false.
Sig wipe!

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Velocity
« Reply #2 on: December 07, 2011, 09:48:33 am »
Example? (I run off examples xD)

Offline LincolnB

  • Check It Out Now
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1115
  • Rating: +125/-4
  • By Hackers For Hackers
    • View Profile
Re: Velocity
« Reply #3 on: December 07, 2011, 10:32:20 am »
If you're referring to acceleration, deceleration, and gravity, those three are actually fairly easy to implement.

Say you have a sprite for your character in Pic1, and the characters position in two variables, X and Y. Every frame, to update the screen, you go like this:

Code: [Select]
.Initialize
0->X->Y

.Main Game Loop
Repeat getkey(15)

ClrDraw
Pt-On(X/256,Y/256,Pic1)

Dispgraph

.Just so it's not too fast...
Pause 75

End

Because you'll probably want to use 256x inflation, to insure smoothness and acuracy. Some say 256x is too much, but dividing by 256 is a highly optimized procedure (read: it's way fast)

So to do gravity, you need to implement constant acceleration along the Y-Axis. To do that, you need a variable to store the current velocity on the Y-Axis, and add to it every frame. I'm going to use the variable E.

Adding to our previous code:

Code: [Select]

0->X->Y->E

Repeat getkey(15)

.Add Y_Velocity to Y_Position
Y+E->Y

.Add a constant acceleration to Y_Velocity
E+100->E

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End

If you run that code, you'll notice a problem - the character falls right off the bottom of the screen! You need to add some code to prevent that.

Code: [Select]

0->X->Y->E

Repeat getkey(15)

Y+E->Y
E+100->E

.Make sure he doesn't fall off the bottom of the screen
If Y>>14336
.^^make sure it's >>, not >.  >> works on negative numers, > doesn't.
.14336 = 56 (the lowest we want our character to be able to go) * 256 (256x inflation)

14336->Y
0->E
.there are other ways of stopping the character, like in Builderboy's Zedd physics library, objects bounce when they hit the ground and stuff like that.

End

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End


Now, we want to add the capability to fly. I'm going to add some code that adds the functionality to fly. If, when we want to fall, we just make E a bigger number, to fly, we just need to make E a negative number, thus subtracting from and making smaller the Y position variable.

Code: [Select]

0->X->Y->E

Repeat getkey(15)

Y+E->Y
E+100->E

If Y>>14336
14336->Y
0->E
End

.Press [2nd] to fly
If getkey(54)

E-150->E
.This number needs to be bigger than 100, to overcome gravity

End

.Make it so the character doesn't fly off the top of the screen
If Y<<0
0->Y->E
End

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End


Great! Now, we need to add acceleration on the X axis. We need a variable for X axis velocity. I'll use D. To do this, when the user presses right, we want to add to the X velocity, and when the user presses left, we want to subtract from it. Piece of cake!

Code: [Select]

0->X->Y->E

Repeat getkey(15)

.Every frame, add X_Velocity to X_position
X+D->X

Y+E->Y
E+100->E

If Y>>14336
14336->Y
0->E
End

If getkey(54)
E-150->E
End

If Y<<0
0->Y->E
End

.When the user presses right, add to X_Velocity
If getkey(3)
D+50->D
End

.When the user presses left, subtract from it
If getkey(2)
D-50->D
End

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End


You can already see the problem here - the character will go off the edges of the screen. We need to add boundary checking.

Code: [Select]

0->X->Y->E

Repeat getkey(15)

X+D->X
Y+E->Y
E+100->E

If Y>>14336
14336->Y
0->E
End

If getkey(54)
E-150->E
End

If Y<<0
0->Y->E
End

If getkey(3)
D+50->D
End

If getkey(2)
D-50->D
End

.If the character is off the right edge...
If X>>22528
.bring him back
22528->X
0->D
End

.If the character is off the left edge...
If X<<0
.we can take care of this
0->X->D
End

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End


Now, we need one final feature - automatic deceleration. Right now the character never loses his energy, and that's not very practical for most games. (note that it's not practical for MOST games - you could still design some kind of a game around that concept)

The way we're going to do this is by checking to see if the user DIDN'T press right or left, decelerate. There are very efficient ways to do this, but for clarity purposes, I'm going to use a very simple method.

Code: [Select]

0->X->Y->E

Repeat getkey(15)

X+D->X
Y+E->Y
E+100->E

If Y>>14336
14336->Y
0->E
End

If getkey(54)
E-150->E
End

If Y<<0
0->Y->E
End

If getkey(3)
D+50->D
End

If getkey(2)
D-50->D
End

If X>>22528
22528->X
0->D
End

If X<<0
0->X->D
End

.if the user isn't pressing right and the user isn't pressing left
If getkey(2)=0 and (getkey(3)=0)
.divide the acceleration value by 2
D//2->D
End

ClrDraw
Pt-On(X/256,Y/256,Pic1)
Dispgraph
Pause 75

End


That should be everything! You now have a simple but working physics movement engine that supports boundary checking, gravity, and acceleration/deceleration.

Keep in mind, take all this code with a grain of salt. It's not perfect, it's not the best way, it doesn't have every little detail you want to add to make games great, but it works! :)
« Last Edit: December 07, 2011, 10:35:45 am by buttsfredkin »
Completed Projects:
   >> Spacky Emprise   >> Spacky 2 - Beta   >> Fantastic Sam
   >> An Exercise In Futility   >> GeoCore

My Current Projects:

Projects in Development:
In Medias Res - Contest Entry

Talk to me if you need help with Axe coding.


Spoiler For Bragging Rights:
Not much yet, hopefully this section will grow soon with time (and more contests)



Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Velocity
« Reply #4 on: December 07, 2011, 02:29:00 pm »
Thanks, I will write notes when I get home! :D

Offline saintrunner

  • Custom Spriter: You ask it! I'll Make it!
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1787
  • Rating: +115/-11
  • Flogging Molly
    • View Profile
    • Jonny K Music
Re: Velocity
« Reply #5 on: December 07, 2011, 03:19:34 pm »
Great tutorial, lol, I could have used this a while ago! I like all the explaining :)
My Sprites Thread   :Updated often :) for your viewing pleasure

GAMES:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Velocity
« Reply #6 on: December 07, 2011, 05:50:00 pm »
In short, just for moving X with no boundaries

Repeat getkey(15)
If getkey(2)
V-15->V
End
If getkey(3)
V+15->V
End
V+X->X
Pt-On(X/256,40,Pic1)
End

Or I use an acceleration variable as well, which can also be used to do that.
For acceleration, if
A=5, it will accelerate by 5 subpixels every frame.
Accel could be used like
A+V->V
V+X->X
« Last Edit: December 07, 2011, 05:53:05 pm by epic7 »

Offline parserp

  • Hero Extraordinaire
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1455
  • Rating: +88/-7
  • The King Has Returned
    • View Profile
Re: Velocity
« Reply #7 on: December 07, 2011, 05:58:17 pm »
BUTTSFREDKIN I LOVE YOU NOW...
(in a manly way... :P)

Offline LincolnB

  • Check It Out Now
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1115
  • Rating: +125/-4
  • By Hackers For Hackers
    • View Profile
Re: Velocity
« Reply #8 on: December 07, 2011, 06:45:12 pm »
BUTTSFREDKIN I LOVE YOU NOW...
(in a manly way... :P)

lol I certainly hope so.
Completed Projects:
   >> Spacky Emprise   >> Spacky 2 - Beta   >> Fantastic Sam
   >> An Exercise In Futility   >> GeoCore

My Current Projects:

Projects in Development:
In Medias Res - Contest Entry

Talk to me if you need help with Axe coding.


Spoiler For Bragging Rights:
Not much yet, hopefully this section will grow soon with time (and more contests)



Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Re: Velocity
« Reply #9 on: December 07, 2011, 09:40:13 pm »
Now I feel stupid.
How would I get this to go backwards (i guess)
Im trying to get my sprite to jump fast up and slow down, then fall.

Offline leafy

  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1554
  • Rating: +475/-97
  • Seizon senryakuuuu!
    • View Profile
    • keff.me
Re: Velocity
« Reply #10 on: December 07, 2011, 10:15:48 pm »
When you hit the jump key, set the y-velocity to a negative value (this can vary depending on how high you want the jump to be, and make sure the y-velocity is stored in a signed variable). Then, every frame add your gravity constant, add the y-velocity to the y-position, and you should be set.
In-progress: Graviter (...)

Offline parserp

  • Hero Extraordinaire
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1455
  • Rating: +88/-7
  • The King Has Returned
    • View Profile
Re: Velocity
« Reply #11 on: December 07, 2011, 11:12:23 pm »
When you hit the jump key, set the y-velocity to a negative value (this can vary depending on how high you want the jump to be, and make sure the y-velocity is stored in a signed variable). Then, every frame add your gravity constant, add the y-velocity to the y-position, and you should be set.
leafy you just made me go "ah hah!"
who knew it was so simple? ;D

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Velocity
« Reply #12 on: December 07, 2011, 11:13:37 pm »
Meeeee! :P

Yeah, I thought it might be a bit tricky for gravity, bt I started and I noticed how really simple it was :P
« Last Edit: December 07, 2011, 11:17:39 pm by epic7 »

Offline parserp

  • Hero Extraordinaire
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1455
  • Rating: +88/-7
  • The King Has Returned
    • View Profile
Re: Velocity
« Reply #13 on: December 07, 2011, 11:36:59 pm »
Meeeee! :P

Yeah, I thought it might be a bit tricky for gravity, bt I started and I noticed how really simple it was :P
yeah lol

how would I implement a generic "stop  falling if there is a pixel below you" with this? (It works when I fall just a few pixels to it, but when falling from great heights it accelerates through it).
I don't understand bboy's virtual pixel testing. D:

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Velocity
« Reply #14 on: December 07, 2011, 11:40:29 pm »
Where does he say that?

I haven't gotten to pxl-testing yet in grappler, just  coordinate checks :P

Butts said it would be hard to do collisions with good gravity