+Philsan Posted December 5, 2022 Share Posted December 5, 2022 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 Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted December 5, 2022 Share Posted December 5, 2022 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 2 Quote Link to comment Share on other sites More sharing options...
Irgendwer Posted December 5, 2022 Share Posted December 5, 2022 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. 1 Quote Link to comment Share on other sites More sharing options...
TGB1718 Posted December 6, 2022 Share Posted December 6, 2022 14 hours ago, TGB1718 said: I did something similar in an Art program I wrote in BASIC many moons ago I found the program, but I was mistaken, there are 3 speeds of cursor movement, but by manually selecting them via the keyboard. Quote Link to comment Share on other sites More sharing options...
Wrathchild Posted December 6, 2022 Share Posted December 6, 2022 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. 2 Quote Link to comment Share on other sites More sharing options...
TheSPY Posted August 11 Share Posted August 11 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 ? Quote Link to comment Share on other sites More sharing options...
Rybags Posted August 12 Share Posted August 12 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. 2 Quote Link to comment Share on other sites More sharing options...
dmsc Posted August 12 Share Posted August 12 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! 1 Quote Link to comment Share on other sites More sharing options...
Wrathchild Posted August 12 Share Posted August 12 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. 1 Quote Link to comment Share on other sites More sharing options...
TheSPY Posted August 12 Share Posted August 12 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 🙂 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.