Author Topic: Top down movement with some tweening.  (Read 471 times)

Offline Bluepixel

  • Mc. Print
  • *
  • Posts: 26
  • www.bluepixel-studios.net
    • View Profile
    • Bluepixel Studios
Hi there, I am working on an top down stealth game and I wanted to create a smooth top down movement, I wanted to add quick easing to the movement, Tho so far I have only been able to hardcode it in.

This is what I have so far:
Code: (glbasic) [Select]
ALLOWESCAPE TRUE
width=1024
height=768
SETSCREEN width, height, 0 // Set screen resolution

LOADSPRITE "Media/player.bmp", 0 // Load all sprites and sounds


setup: // Setup all variables and other systems
px=width/2-64 // Player X
py=height/2-64 // Player Y
pspd=4 // Player speed
GOTO main


main: // The main loop
GOSUB move

DRAWSPRITE 0, px, py
SHOWSCREEN
GOTO main


SUB move:
IF KEY(17) // Add simple WASD movement
py=py-pspd
ENDIF

IF KEY(31)
py=py+pspd
ENDIF

IF KEY(30)
px=px-pspd
ENDIF

IF KEY(32)
px=px+pspd
ENDIF

RETURN
ENDSUB

Can anyone help me with this?
« Last Edit: 2019-Sep-14 by Bluepixel »
www.bluepixel-studios.net

There are two ways to write error-free programs; only the third one works.
(Alan J. Perlis)

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4336
    • View Profile
    • FUED
Re: Top down movement with some tweening.
« Reply #1 on: 2019-Sep-15 »
I do it with a variable for speed and another for acceleration.
For example, PX and PY for player position, PSX and PSY for their speed and PAX and PAY for acceleration.

On the loop, I add acceleration to the speed:
PSX=PSX+PAX
PSY=PSY+PAY

Then I have this to add the final speed to the positions:
PX=PX+PSX
PY=PY+PSY

Then I have another set of lines to add friction per cycle so the object has a tendency to stop as time goes by if no direction is pressed:
if PSX>0 then PSX=PSX-.1
if PSX<0 then PSX=PSX+.1
if PSY>0 then PSY=PSY-.1
if PSY<0 then PSY=PSY+.1

With the WASD keys, I alter the acceleration, if no key pressed, acceleration is 0.
You have to control max speed and a few other bits but this should give an idea.
The last .1 values could also be a variable in case the terrain under the player changes and you need more or less friction, say ice or sand.
Cheers.


Then I add