Jump to content
IGNORED

FastBasic cursor movement with acceleration


Recommended Posts

How would you move a sprite representing a pointer/cursor with acceleration?

 

This is my classic code but the cursor is too fast.

At first it should move slowly then accelerate.

 

proc READ_JOYSTICK
    j=stick(0)
    if (J & 8) = 0 then x0 = x0 + 1
    if (J & 4) = 0 then x0 = x0 - 1
    if (J & 2) = 0 then y0 = y0 + 1
    if (J & 1) = 0 then y0 = y0 - 1
    endproc

proc CHECK_BOUNDARIES
    if x0>203 then x0=203
    if x0<44 then x0=44
    if y0>230 then y0=230
    if y0<8 then y0=8
    endproc  

proc MOVE_SPRITES
     poke 53248,x0
     move adr(Player0),pmbp0+y0,18
     endproc 

 

Link to comment
Share on other sites

I think you would need to read the joystick value, store it, on the next read, see if it's the same value

maybe set a counter so you can accelerate movement if same direction for "n" reads.

 

I did something similar in an Art program I wrote in BASIC many moons ago, will see if I can find

it, it moves a sprite pointer based on joystick reads too 

  • Like 2
Link to comment
Share on other sites

2 hours ago, Philsan said:

This is my classic code but the cursor is too fast.

At first it should move slowly then accelerate.

You can just apply some kind of pause, put into the function "READ_JOYSTICK".

If the stick isn't moved at all (=15) set the duration of the pause to a high value, else reduce the value as long as you think it's enough.

This way you can also move the object fast arround, as long there is only a transition, not a break, in movement.

  • Like 1
Link to comment
Share on other sites

18 hours ago, Philsan said:

How would you move a sprite representing a pointer/cursor with acceleration?

Quite often you can use a co-ordinate range larger than that of the screen, e.g. x4, and then quick divide to put in range when you want to draw.

 

Then you use a 'velocity' variable, i.e. 0 is stationary. When you detect movement, increment this and then set a countdown delay (e.g. # of vb cycles).

So the increment can work only if the delay is zero and less than the cap, e.g. 4.

 

Then each vb you can add the velocity to x,y coordinates, so you have one velocity each for up/down, left/right and hence negative for up/left.

 

You then have the option of ceasing movement (joystick centred) either resetting the velocities to zero (immediate stop) or use the vb to move the velocity toward zero based on another counter to provide deceleration.  

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Sorry slightly off typic, but can some one explain to me the use of "&" in basic 

 

IE in Philsan post above they read the joystick in to J and the use that with and & to determine  the direction pressed.

 

proc READ_JOYSTICK
    j=stick(0)
    if (J & 8) = 0 then x0 = x0 + 1
    if (J & 4) = 0 then x0 = x0 - 1
    if (J & 2) = 0 then y0 = y0 + 1
    if (J & 1) = 0 then y0 = y0 - 1
    endproc

where as I would alway do it like this 

 

proc READ_JOYSTICK
    j=stick(0)
    if (J) = 7 then x0 = x0 + 1
    if (J) = 11 then x0 = x0 - 1
    if (J) = 14 then y0 = y0 + 1
    if (J) = 13 then y0 = y0 - 1
    endproc

 and this works for me.  So what is the advantage of using the & and how does it work ? 

I have seen other people do similar, but never really understood how this works or the advantages ?

 

Link to comment
Share on other sites

To do acceleration, have an array that contains the position increment values (including negatives)

Then with your stick read, have a pointer into the array that slides along the set of values (e.g. use 9 values with the middle one being 0 for static such as -3,-2,-1,-0.5,0,0.5,1,2,3

If the stick is centred, move the pointer towards the middle otherwise left/right moves the pointer towards each extreme.

 

The values you use might need to be fractional depending on how fast the program executes.  And of course you need the bounds checking stuff to either wrap around or stop the object.

  • Like 2
Link to comment
Share on other sites

Hi!

5 hours ago, TheSPY said:

Sorry slightly off typic, but can some one explain to me the use of "&" in basic 

 

IE in Philsan post above they read the joystick in to J and the use that with and & to determine  the direction pressed.

 

proc READ_JOYSTICK
    j=stick(0)
    if (J & 8) = 0 then x0 = x0 + 1
    if (J & 4) = 0 then x0 = x0 - 1
    if (J & 2) = 0 then y0 = y0 + 1
    if (J & 1) = 0 then y0 = y0 - 1
    endproc

where as I would alway do it like this 

The above reads the bits that specify each of the switches inside the joystick, one switch for each direction, and can detect multiple switches pressed at the same time - i.e., diagonal movements. "&" is the binary AND operator, in this case it test a specific bit in the value.

 

5 hours ago, TheSPY said:
proc READ_JOYSTICK
    j=stick(0)
    if (J) = 7 then x0 = x0 + 1
    if (J) = 11 then x0 = x0 - 1
    if (J) = 14 then y0 = y0 + 1
    if (J) = 13 then y0 = y0 - 1
    endproc

 

The above only compare some of the possible values, so it ignores diagonals.

5 hours ago, TheSPY said:

 and this works for me.  So what is the advantage of using the & and how does it work ? 

I have seen other people do similar, but never really understood how this works or the advantages ?

The advantage is that the diagonal movements are correctly handled.

 

Also, you can abbreviate the conditions as:

proc READ_JOYSTICK
    j=stick(0)
    if NOT (J & 8) then INC x0
    if NOT (J & 4) then DEC x0
    if NOT (J & 2) then INC y0
    if NOT (J & 1) then DEC y0
endproc

 

Or even the shorted:

proc READ_JOYSTICK
    J = stick(0)
    x0 = x0 + (NOT J & 8) - NOT J & 4
    y0 = y0 + (NOT J & 2) - NOT J & 1
endproc

 

Have Fun!

 

  • Like 1
Link to comment
Share on other sites

On 12/5/2022 at 7:05 PM, Philsan said:

with acceleration

You can use a larger co-ordinate area, e.g. 4 * larger than the screen, then when moving add a velocity, initially 1, and increase that after a period of time to a max, e.g. 4.

Then when displaying the sprite, divide the world-coordinate to be a screen-coordinate, e.g. / 4.

Also, when movement has stopped, rather than setting velocity directly to 0, you can use a timer to decrement it down to zero to simulate deceleration. 

  • Like 1
Link to comment
Share on other sites

20 hours ago, dmsc said:

Hi!

The above reads the bits that specify each of the switches inside the joystick, one switch for each direction, and can detect multiple switches pressed at the same time - i.e., diagonal movements. "&" is the binary AND operator, in this case it test a specific bit in the value.

 

The above only compare some of the possible values, so it ignores diagonals.

The advantage is that the diagonal movements are correctly handled.

 

Also, you can abbreviate the conditions as:

proc READ_JOYSTICK
    j=stick(0)
    if NOT (J & 8) then INC x0
    if NOT (J & 4) then DEC x0
    if NOT (J & 2) then INC y0
    if NOT (J & 1) then DEC y0
endproc

 

Or even the shorted:

proc READ_JOYSTICK
    J = stick(0)
    x0 = x0 + (NOT J & 8) - NOT J & 4
    y0 = y0 + (NOT J & 2) - NOT J & 1
endproc

 

Have Fun!

 

ok, this make sense now, the way I have been doing it would require 8 lines of code to read all the possible directions of the stick.. But with using the binary & it can be reduced down to just 2 lines, as per your final example 🙂

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...