Famicom BASIC mathematical functions

Started by zmaster18, October 08, 2015, 09:25:03 am

Previous topic - Next topic

zmaster18

October 08, 2015, 09:25:03 am Last Edit: October 08, 2015, 11:14:49 am by zmaster18
Today I'm trying to create some mathematical programs that will enhance my game that is going to be finished soon. I need to use the equation of a circle:

x^2 + y^2 = r^2          (x and y are the center coordinates of the circle, r is the radius)

In order to actually solve the equation for y, given x and r, I need to use a square-root function. There is no square root function in Famicom BASIC, so I actually need to make one. Has anyone ever made their own square root function before? I kind of have an idea of how it is done:

-Get the value you want to square root
-make a guess
-see if the guess is correct. If not, iterate the guess and loop until correct

In order to check the guess, times the guess by itself and see if it equals the value you want to square root. This doesn't need to be precise because BASIC just uses integer values.

I'm going to try doing this later today and show you guys my findings.

chowder

Mathematically intensive operations on 8 bit consoles are S-L-O-W, and that's in assembly.  The usual way of doing it is to use tables of pre-computed values if possible.  Just store the square roots of the range of numbers you'll be using in a table in order, and then index in to it using the value.  I'm assuming this is possible in Famicom BASIC, I have no experience in it whatsoever :)

UglyJoe

The "proper" solution would be to write the method in assembly so that you could use floating point.

If you're sticking with BASIC, then you can either write a method or lookup table.  Method will be slow, but the table will eat up memory (gotta store the table somewhere).

In this specific case, I would try to write a more specific solution rather than a general solution.  Probably would go with a table, but try to reduce the size of it as much as possible.  What are the restrictions on your variables (min/max x, min/max y)?  The smaller the range, the smaller the lookup table.  If you're going to settle for integer results only, then that also reduces the table size (since it's a range of values pointing to the same result, rather than a one-to-one relationship).

zmaster18

October 08, 2015, 11:30:25 am #3 Last Edit: October 08, 2015, 06:25:34 pm by zmaster18
I thought about lookup tables before, but I don't want to risk using that much memory. I think I actually could just do the calculations in-program because the size of my radius for the circle is only going to be 16 pixels. So this would only be 16 values (for - and +) for x to plug in to my square-root function. The loop would only have to iterate 16 times for each guess, right? And then the sign of the result could be altered using the SGN command depending on the condition. I suppose I could POKE the 16 values and then access them with PEEK &address + x.
Here's what my circle equation will look like:

y = +/- sqrt(256 - x^2)             (256 is r^2, so 16^2)

x will range from 0 to 16 and then I will determine the sign of the answer based on where the position of the point on the circle's circumference is. I'm using this this to have a sprite rotate around a point. You press the right arrow key to increase x, and left arrow key to decrease x.



Post Merge: October 08, 2015, 11:47:50 am

I just tried poking 17 values (0 to16 as well) and it takes just 55 bytes. I think this is the best way. The answer to the equation is simply:

y = +/- PEEK &address +x

The circle is divided in 4 quarters and to determine which quarter of the circle is depends on the sign of x or y. I think I got this solved! :)

I haven't done math stuff like this since high school. I really need to relearn my geometry, graphing, physics, etc if I want to program cool effects in games! For example, I need to learn the equation of the sinusoidal wave if I want to make something like Samus' Wave Beam or a flying Medusa Head. My game I'm working on now does involve acceleration, which is change in speed. Change in speed in BASIC is simply just how fast or slow you want to animate the sprites. I can easily do this with using PAUSE and adjusting the value to slow it down between frames of animation.

We can still use this thread to discuss other equations and making cool movement effects.

DahrenDreamcast

November 12, 2015, 12:58:42 pm #4 Last Edit: November 12, 2015, 02:22:50 pm by DahrenDreamcast
Had a play in basic myself today....I'm progressing slowly. Seeing how advanced some of the threads on basic are here is beautiful  :)
How you getting on with it Z? it sounds exciting...

Nintendo Child <3

zmaster18

I have finished making the circle drawing function here! :) It works well and I can use it in my game. The game, which is going to be a surprise, is getting near completion. I am so excited to almost be finished a completed game  8)

DahrenDreamcast

Well done, I am so pleased for you! and excited to see it soon  :)
Nintendo Child <3

P

Your game sounds quite advanced. I'm excited to see what it's like too! :)