Jump to content

Recommended Posts

I'm making a game in which the character (player0) is moving by itself automatically. So by putting x=x-j for going left, in which x is player0x and j is 1, I've discovered that j being 1 is too slow, while j being 2 is too fast. So, is there a way to make j=1.5? Or is that impossible with bBASIC?

Link to comment
https://forums.atariage.com/topic/92700-quick-movement-question/
Share on other sites

Here's how it's done.

 

First, you'll need to define a fixed-point decimal variable somewhere near the top of your code. Each fixed-point decimal value will take up two of your 8-bit variables, but it'll give you the accuracy you'll need to move across the entire screen.

 

player0x, player0y, player1x, etc are all simple 8-bit integer values. Because of that, you'll have to store and maintain any decimal coordinates separate from the system sprite coordinates. When you copy your decimal coordinate to the sprite coordinate, it'll ignore the decimal part and just give you the integer.

 

Here's what the code will look like:

 

 rem Place this line near the beginning of your code.
 dim px=a.b

 rem This is what moving the player 1.5 pixels in the negative x direction looks like.
 px = px - 1.5
 player0x = px

 

Notice that the new variable px uses up both your a and b variables. Any changes you make to a or b will affect px, and probably not in a way you expect unless you understand how binary decimals work.

 

The help file for bBasic .35 has a discussion of how these variables work under "Fixed Point Variables", and the "sample.bas" file has an example of a sprite moving at a fixed-decimal velocity.

I read the help file and saw the sample code and I still don't get a few things: :?

#1 : In the code you gave me, how does it know what a and b are if you haven't set them? (like a=#, b=#)

#2 : In my code, the missile0x=px (it's missile for me) code loops back to a missile0x=x : missile0y=y line, and px would equal j, because x=x-j. For this code, I'd like the missile to speed up, so I would put x=x-j if I set j to 1, j to 1.5 (which isn't doable), j=2, etc. So how would that work? I couldn't make j be px and say x=x-px because I'd have to say it would equal j, right?

#2a : What do I change the missile0x=x : missile0y=y line to?

#2b : Do I need a seperate py code for the y variable?

#2b : And also with the j, the lines to make them move are like this:

I set g=1 through 4 for each direction. In this example, 1 stands for left.

 

245 if g=1 then x=x-j

if collision(missile0,playfield) then l=l-1 : goto 350

if collision(missile0,player1) then score=score+1 : m=m+1: goto 110

 

and after all those, it says goto 120

 

so how would I change line 120 which it says now : j=1 (which I would want to say if m>19 then (move missile0x at speed 1.5).

 

So the question is: how do I set j to be 1.5 using an 8.8 type (and switch back and forth between a fixed point variable to a regular variable)

Given I've only have a few months experience with bBASIC (or any BASIC language for that manner,) would I understand this or am I in way over my head here?

I hope I haven't confused anyone with this post.

  • 1 month later...

OK, new question: How do I make b=b+.25?

 

Without using the fixed point math, posted above, you can do the following:

 

 

 

Use a delay loop, so that the movement is only updated every so many frames.

 

Setup a variable counter that counts from 0 to 3, or whatever you think is good. Let's say it's b, for now.

 

Instead of updating your movement every frame, do this instead:

 

if b = 3 then b=0 : x = x + 1

 

This will only happen every four screens, thus your movement will be 1/4 the full speed. 1/4 = .25

 

If you made the count three, instead of 4, then you would get 1/3 the full speed, etc....

 

You can vary the counter to change movement speeds as multiples of your 60 frames per second game loop. You can also change the x value from 1 to 2 or maybe 4 max, to get other movements. Using the two together gives you a fair number of different movement speeds possible, with only simple whole 8 bit number math.

 

 

Another approach is to divide your movement before actually doing it.

 

say x is the position to be moved, and you are adding 1 every frame.

 

If you do this player1x = x / 2, then you essentially are moving every other frame, as in the example above.

 

This only works for multiples of two, so x/2 x/4 x/8, etc.. are ok. x/3 is not, err is not the last time I tried it in bB.

 

Think of binary numbers:

 

0000 = 0

0001 = 1

0010 = 2

0011 = 3

0100 = 4

 

etc....

 

a divide by two is just like lopping off the right most digit.

 

000 = 0

000 = 0

001 = 1

001 = 1

010 = 2

 

etc...

 

All of this works for simple linear movements. Does not work for anything more complex, because it takes too many loops and divides to make any solid sense. If you need intertia, curves, accelleration, etc... Better to use the fixed point math.

 

The divide takes more ROM program space, and the counter consumes a variable.

 

The advantage of the divide is that you don't have to give up one of your few RAM bytes to get things done. An advantage of the counter is that you can use it for lots of things. It can control sounds, be used to trigger more than one movement (just need more than one if statement), etc... I used these techniques for my early game Ooze. Largely because I needed the space and the fixed point stuff was just not where it needed to be. You can find code in my blog. Some of the early versions are a lot simpler to read, BTW.

 

It's a messy program, but it's got some of that stuff in there, along with flags.

Edited by potatohead

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...