MATH

 

Randomize

This command reseeds the random number generator with a given number. CoolBasic does it automatically on each start.

Usage:

Randomize number

Example:

Randomize 5

Randomize Timer()

 

Int

Converts a float number or string into an integer.

Example:

Print Int(6.7)

 

Float

Converts an integer string into a float number.

Example:

Print Float("4.4")+ (-1)

 

RoundUp

Rounds a float number up to the nearest Integer

Example:

Print RoundUp(-3.3)

Print RoundUp(55.5)

 

RoundDown

Rounds a float number down to the nearest Integer

Example:

Print RoundDown(-10.1)

Print RoundDown(1.9)

 

Abs

Returns the absolute value of the given number

Example:

Print Abs(-5)

 

Sqrt

Returns the square root of the given number i.e. the value that needs to be multiplied by itself in order to get the original result value.

Example:

Print Sqrt(4)

 

Sin

Returns the sine of the given angle. The angle is measured in degrees.

 

Cos

Returns the cosine of the given angle. The angle is measured in degrees.

 

Tan

Returns the tangent of the given angle. The angle is measured in degrees.

 

ASin

Returns the arcus sine of the given number. The angle is measured in degrees.

 

ACos

Returns the arcus cosine of the given number. The angle is measured in degrees.

 

ATan

Returns the arcus tangent of the given number. The angle is measured in degrees.

 

GetAngle

Tells at which angle point B is from the view of point A.

Usage:

angle# = GetAngle(x1, y1, x2, y2)

Example:

Repeat

    Line 200,150,350,150

    Line 200,150,MouseX(),MouseY()

    Text 0,0,GetAngle(200,150,MouseX(),MouseY())

    DrawScreen

Until EscapeKey()

 

GetAngle2

Tells at which angle object B is from the view of object A.

Usage:

angle# = GetAngle(obj1, obj2)

Example:

DrawToWorld ON

FrameLimit 40

obj1=LoadObject("Media\guy.bmp")

obj2=LoadObject("Media\guy.bmp")

AddText "The code updates every 5 seconds..."

Repeat

    'Perform this every 5 seconds

    If TIMER()>moment+5000 Then

        PositionObject obj1,Rand( -180,180),Rand( -130,130)

        PositionObject obj2,Rand( -180,180),Rand( -130,130)

        'find out the angle between obj1->obj2

        angle=GetAngle2(obj1,obj2)

        moment=TIMER()

    EndIf

    Line ObjectX(obj1),ObjectY(obj1),ObjectX(obj1)+Cos(angle)*40,ObjectY(obj1)+Sin(angle)*40

    Text 0,30,angle

    DrawScreen

Until EscapeKey()

 

Log

Natural Logarithm of X

Usage:

result# = Log(X#)

 

Log10

Common Logarithm of X

Usage:

result# = Log10(X#)

 

Rnd

Returns a random float number on the given range

Usage:

random# = Rnd([min#,] max#)

 

Rand

Returns a random integer number on the given range

Usage:

I = Rand([min,] max)

 

Min

Returns the smallest of two provided numbers

Usage:

smallest = Min(valu1, value2)

Example:

Print Min(10, -5)

 

Max

Returns the largest of two provided numbers

Usage:

largest = Max(valu1, value2)

Example:

Print Max(10, -5)

 

CurveValue

This function interpolates a value rounding it to the target smoothly. It might make your game look smoother. See the example.

Usage:

newValue# = (targetValue#, oldValue#, smoothness#)

Example:

FrameLimit 40

AddText "The two circles will follow the mouse smoothly"

Repeat

    'less curving

    x1#=CurveValue(MouseX(),x1#,5.0)

    y1#=CurveValue(MouseY(),y1#,5.0)

    'more curving (smoother)

    x2#=CurveValue(MouseX(),x2#,20.0)

    y2#=CurveValue(MouseY(),y2#,20.0)

    Color cbyellow

    Circle x2-10,y2-10,20

    Color cbred

    Circle x1-8,y1-8,16

    DrawScreen

Until EscapeKey()

 

CurveAngle

The same thing as CurveValue, but keeps it at range 0...360

 

WrapAngle

Takes an angle, and returns it wrapped at range 0...360. For example if you provide angle 565, the result would be 205

Usage:

newAngle# = WrapAngle(oldAngle#)

 

Distance

Tells the distance between two points. In pixels.

Usage:

dist# = Distance(X1, Y1, X2, Y2)

Example:

AddText "Screen size is 400x300"

Repeat

    Circle 190,140,20,OFF

    Line 200,150,MouseX(),MouseY()   

    Text 20,20,"Distance to mouse is: "+Distance(200,150,MouseX(),MouseY())

    DrawScreen

Until EscapeKey()

 

Distance2

Tells the distance between two objects. In game world units i.e. Pixels.

Usage:

dist# = Distance2(obj1, obj2)

Example:

Dim targetx As Float

Dim targety As Float

Dim obj2x As Float

Dim obj2y As Float

DrawToWorld ON

FrameLimit 40

obj1=LoadObject("Media\guy.bmp")

obj2=LoadObject("Media\guy.bmp")

AddText "The code updates every 3 seconds..."

Repeat

    'Perform this every 3 seconds

    If TIMER()>moment+3000 Then       

        targetx=Rnd( -180,180)

        targety=Rnd( -130,130)       

        moment=TIMER()

    EndIf

    'curve location, round it against the target

    obj2x=CurveValue(targetx,obj2x,20.0)

    obj2y=CurveValue(targety,obj2y,20.0)

    PositionObject obj2,obj2x,obj2y

    Text 0,30,"Distance: "+Distance2(obj1,obj2)

    DrawScreen

Until EscapeKey()

 

BoxOverlap

Tells if two boxes overlap. See the example.

Usage:

result = BoxOverlap(box1X, box1Y, box1Width, box1Height, box2X, box2Y, box2Width, box2Height)

Example:

AddText "Use arrows to move around"

'first generate two random sized boxes

box1width=Rand(20,100)

box1height=Rand(20,100)

box2width=Rand(20,100)

box2height=Rand(20,100)

Repeat

    'update controls

    If LeftKey()Then x=x-1

    If RightKey()Then x=x+1

    If UpKey()Then y=y-1

    If DownKey()Then y=y+1

    'box 1 (movable)

    Box x,y,box1width,box1height,OFF

    'box 2 (static)

    Box 260,180,box2width,box2height,OFF

    Text 20,20,"Overlapping: "+BoxOverlap(x,y,box1width,box1height,260,180,box2width,box2height)

    DrawScreen

Until EscapeKey()