Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
mathematical functions in games
#1
I've noticed that some examples related to my questions use the min and max functions.

I don't usually use any mathematical functions because I don't know how I could apply them in a game.

I'd like to know which mathematical functions you most often use in your games and how you use them. If you'd like, you can show small examples of how you use these functions in your games or anything else you'd like to share.
Reply
#2
Math Functions
  • (number)rnd() Returns a random number in the range [0 .. 1]
  • (number)round((number) Returns the integer value that is closest to x.

Code:
' Simple Addition Game
'=====================

' Clear screen
set color 0,0,0; cls
set color 255,255,255
wln "Simple Addition Game";wln

' Main Loop
do
    ' Generate two random numbers (1-10)
    a = int(rnd()*10) + 1
    b = int(rnd()*10) + 1
    write a + " + " + b + " = "
   
    ' Get user answer
    ans = rln(2,TYPE_NUMBER)
   
    ' Check answer
    if ans = (a+b)  wln "Correct! :)"
    if ans <> (a+b) wln "Wrong!"
    wln
loop
Reply
#3
I probably use 'sqr' (square root) the most. Want A to move towards B at speed 3?

Code:
adx = bx - ax
ady = by - ay
k  = 3/sqr(adx*adx + ady*ady)
adx = adx*k
ady = ady*k

...

ax = ax + adx
ay = ay + ady
Reply
#4
I'm no mathematician, but with a lot of trial and error, I find sin and cos very useful. Simple example here, but it can easily be amended to create a "steering" system, such as that used in games like asteroids, or to produce bullets that fire in all directions:
Code:
visible screen_w = 640,screen_h = 480;set window "Example",screen_w,screen_h
set redraw off
x1 = 320;y1 = 240';x2 = unset;y2 = unset
angle = 0
do
set color 255,255,255;cls;set color 0,0,0
x2 = x1 + sin(angle) * 100
y2 = y1 - cos(angle) * 100
angle  = angle + 0.01
draw line x1,y1,x2,y2
redraw;fwait 60;until keydown(KEY_ESCAPE)

A thumbs up for using "sqr" - here used to calculate distances between 2 points - great for circle collision detection, among other things.
Code:
visible screen_w = 640,screen_h = 480;set window "Example",screen_w,screen_h
set redraw off
do
set color 255,255,255;cls;set color 0,0,0
draw ellipse 320,240,4,4,1
draw ellipse mousex(),mousey(),4,4,1
set caret 240,20
gap = distance(320,240,mousex(),mousey())
write "distance = " + gap
redraw;fwait 60;until keydown(KEY_ESCAPE)

function distance(x1,y1,x2,y2)
c = abs(x1 - x2)
d = abs(y1 - y2)
dist = sqr(c*c + d*d)
return dist
endfunc
Reply
#5
I like these examples. They are easy to follow and I find them very useful.

When you have the time and feel like it, could you show more examples with mathematical functions? I find them very useful.

(02-01-2026, 05:14 AM)1micha.elok Wrote: Math Functions
  • (number)rnd() Returns a random number in the range [0 .. 1]
  • (number)round((number) Returns the integer value that is closest to x.

Code:
' Simple Addition Game
'=====================

' Clear screen
set color 0,0,0; cls
set color 255,255,255
wln "Simple Addition Game";wln

' Main Loop
do
    ' Generate two random numbers (1-10)
    a = int(rnd()*10) + 1
    b = int(rnd()*10) + 1
    write a + " + " + b + " = "
   
    ' Get user answer
    ans = rln(2,TYPE_NUMBER)
   
    ' Check answer
    if ans = (a+b)  wln "Correct! :)"
    if ans <> (a+b) wln "Wrong!"
    wln
loop

Do you have any examples of round()
Reply
#6
round()

Code:
set window "Snap to Grid",400,300
set redraw off

'initialization
bx=200;by=220;drag=false;ox=0;mb0=0;success=false

'main loop
do
  mx=mousex();my=mousey();mb=mousebutton(0)
 
  if success=false
    if drag=false and mb=1 and mb0=0
      if mx>bx-30 and mx<bx+30 and my>by-30 and my<by+30 then drag=true;ox=bx-mx
    elseif drag=true
      bx=mx+ox;by=my;if mb=0;bx=round((bx-200)/100)*100+200;by=80
      if bx=200 and by=80 then success=true;drag=false;endif
    endif
  endif
 
  set color 50,50,70;cls;set color 255,230,50;draw rect 170,50,60,60,false;set caret 165,120;wln "YELLOW";draw rect bx-30,by-30,60,60,true
 
  if success=true
    set caret 110,210;wln "Press ENTER to restart";temp=rln();bx=200;by=220;drag=false;success=false
  endif
 
  redraw;fwait 60;mb0=mb
loop
Reply
#7
(02-02-2026, 02:11 AM)1micha.elok Wrote: round()

Code:
set window "Snap to Grid",400,300
set redraw off

'initialization
bx=200;by=220;drag=false;ox=0;mb0=0;success=false

'main loop
do
  mx=mousex();my=mousey();mb=mousebutton(0)
 
  if success=false
    if drag=false and mb=1 and mb0=0
      if mx>bx-30 and mx<bx+30 and my>by-30 and my<by+30 then drag=true;ox=bx-mx
    elseif drag=true
      bx=mx+ox;by=my;if mb=0;bx=round((bx-200)/100)*100+200;by=80
      if bx=200 and by=80 then success=true;drag=false;endif
    endif
  endif
 
  set color 50,50,70;cls;set color 255,230,50;draw rect 170,50,60,60,false;set caret 165,120;wln "YELLOW";draw rect bx-30,by-30,60,60,true
 
  if success=true
    set caret 110,210;wln "Press ENTER to restart";temp=rln();bx=200;by=220;drag=false;success=false
  endif
 
  redraw;fwait 60;mb0=mb
loop

I don't understand what this example does.
Reply
#8
round simply returns the nearest integer value. round(13.9) returns 14 and round(13.25) returns 13 Smile

Then you have floor, that always rounds down, and ceil, that rounds up.
Reply
#9
(02-02-2026, 03:12 PM)aliensoldier Wrote: ...
I don't understand what this example does.

In my example, round() converts fractional grid positions into whole numbers.
By normalizing coordinates relative to the grid origin, dividing by cell size, rounding, then scaling back—you force any position to "jump" to the center of the nearest grid cell. Let me give you another example :

Code:
#win32
set window "Snap to Grid v2",400,300
set redraw off

box = []
box.x = 200       ' current box X position
box.y = 220       ' box Y position (fixed)

snapped = false
t = 0        ' time counter for animation

do
  k = inkey()
 
  ' Animate box horizontally to show "unsnappped" movement
  if snapped = false
    t = t + 0.02
    box.x = 200 + sin(t) * 120  ' oscillates between ~80 and ~320
    if k = 32
      box.x = round((box.x - 200) / 100) * 100 + 200
      box.y = 80                ' snap Y to target row
      snapped = true
    endif
  else
    if k = 13
      box.x = 200
      box.y = 220
      snapped = false
    endif
  endif
 
  ' clear screen
  set color 50,50,70; cls
 
  ' target
  set color 255,230,50 ; draw rect 170, 50, 60, 60, false
  set caret 165, 120 ; wln "TARGET"
 
  ' player box
  set color 255,230,50 ; draw rect box.x - 30, box.y - 30, 60, 60, true
 
  ' Instructions
  set color 255,255,255
  set caret 100,260
  if snapped = false
    wln "Press SPACE to snap to grid"
  else
    wln "Press ENTER to Continue"
  endif
 
  redraw
  fwait 60
loop
Reply
#10
Now I understand, thanks.

Do you have more examples like these? They're great...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)