Contains functions such as:
Rotate point
Convex collision
Bouncing
etc.


INSTRUCTIONS
To rotate a point, use RotateX and rotateY to get the new x and y point position.

To check if a point is inside a convex shape, use ConvexCollision. The first x/y values are the location of the point, the others create a line. If it returns 1 when used on every line in the convex shape, the point is in the shape. The lines must go round the center of the shape clockwise.

To bounce a vector off a face, use Bounce. The Vx/y values define the vector to be bounced, and Nx/y values specify a line to bounce the vector off.

Language: 
Dark Basic Pro
Code: 
sync on

global GetBounceX as float
global GetBounceY as float

type xy

    x as integer
    y as integer

endtype

sync : sync
input "Enter number of sides: ",siden

dim MyShape(siden-1) as xy

for d = 0 to siden-1
    ang = (d*(360/siden))+rnd(50/siden)
    dist# = 100+rnd(50/siden)
    MyShape(d).x = (sin(ang)*dist#)+(screen width()/2)
    MyShape(d).y = (cos(ang)*dist#)+(screen height()/2)
next d

do

    msx = mousex()
    msy = mousey()
    Contained = 1
    for d = 0 to siden-1
        c = d-1
        if c<0 then c = siden-1
        x1 = MyShape(c).x
        y1 = MyShape(c).y
        x2 = MyShape(d).x
        y2 = MyShape(d).y
        line x1, y1, x2, y2
        if ConvexCollision(msx, msy, x1, y1, x2, y2)=0
            Contained = 0
        endif
    next d
    if Contained
        ink rgb(255,0,0),0
    else
        ink rgb(255,255,255),0
    endif

    inc BA#,0.2
    line 25-sin(BA#)*10,30-cos(BA#)*10,25+sin(BA#)*10,30+cos(BA#)*10
    line 25,30,msx,msy
    Bounce(msx-25,msy-30,sin(BA#),cos(BA#))
    line 25,30,GetBounceX+25,GetBounceY+30

    sync
    cls

loop

function ConvexCollision(x, y, x1, y1, x2, y2)
    ang1# = atanfull(x2-x1,y2-y1)
    ang2# = atanfull(x-x1,y-y1)
    ang2# = wrapvalue(ang2#-ang1#)
    if ang2#<=180
        exitfunction 1
    endif
endfunction 0

function Bounce(Vx#, Vy#, Nx#, Ny#)
    ang# = wrapvalue(atanfull(Nx#, Ny#))
    NVx# = RotateX(Vx#, Vy#, Ang#)
    NVy# = RotateY(Vx#, Vy#, Ang#)
    NVy# = 0-NVy#
    GetBounceX = RotateX(NVx#, NVy#, 0-Ang#)
    GetBounceY = RotateY(NVx#, NVy#, 0-Ang#)
endfunction

function RotateX(OldX#, OldY#, Angle#)
    NewX# = OldX#*cos(Angle#)-OldY#*sin(Angle#)
endfunction NewX#

function RotateY(OldX#, OldY#, Angle#)
    NewY# = OldX#*sin(Angle#)+OldY#*cos(Angle#)
endfunction NewY#
5
Average: 5 (1 vote)