Some functions that I've made, hope they are useful.
General Functions-Includes functions like freeObject(), followCursor(),angleFromPtToPt(),bezierValue(), and many more
`General Functions
`====================
function freeSprite()
`====================
`Gets an unused sprite number id
repeat
inc id
if sprite exist(id)=0 then found=1
until found
`=============
endfunction id
`=============
`====================
function freeObject()
`====================
`Gets an unused object number id
repeat
inc id
if object exist(id)=0 then found=1
until found
`=============
endfunction id
`=============
`========================
function freeLimb(object)
`========================
`Gets an unused limb number id for a specified object
repeat
inc id
if limb exist(object,id)=0 then found=1
until found
`=============
endfunction id
`=============
`===============================
function controlSync(frameRateF)
`===============================
`Controls the framerate of a program
loopTime#=1000/(timer()-tmpTimer#)
tmpTimer#=timer()
wait (1000/frameRateF)*((loopTime#/frameRateF)/2)
sync
`==========
endfunction
`==========
`====================================
function followCursor(object,cameraF)
`====================================
`Makes an object follow a cursor
objz#=object position z(object)
null=make vector3(1)
null=make vector3(2)
null=make vector3(3)
null=make vector3(4)
set vector3 1,0,0,-1
pick screen mousex(),mousey(),1
x#=get pick vector x()
y#=get pick vector y()
z#=get pick vector z()
set vector3 2,x#,y#,z#
angle#=acos(dot product vector3(2,1))
cosAngle#=cos(angle#)
hypoLength#=camera position z(cameraF)/cosAngle#
normalize vector3 2,2
scale vector3 2,2,hypoLength#
set vector3 1,camera position x(cameraF),camera position y(cameraF),camera position z(cameraF)
add vector3 4,1,2
position object object,x vector3(4),y vector3(4),objz#
`==========
endfunction
`==========
`==========================
function getMaxSize(object)
`==========================
`Checks which dimension is greatest for an object: x, y,or z and returns the
`value for that dimension
sizex#=object size x(object,1)
sizey#=object size y(object,1)
sizez#=object size z(object,1)
if sizex#>=sizey#
maxSize#=sizex#
else
maxSize#=sizey#
endif
if maxSize#<sizez#
maxSize#=sizez#
endif
`===================
endfunction maxSize#
`===================
`====================================
function getDistance(x1#,y1#,x2#,y2#)
`====================================
`Gets the distance between two points
xtemp#=(x1#-x2#)^2
ytemp#=(y1#-y2#)^2
distance#=sqrt(xtemp#+ytemp#)
`====================
endfunction distance#
`====================
`====================================
function anglePtToPt(x1#,y1#,x2#,y2#)
`====================================
`Gets the angle from one point to another, x1 and y1 being the coordinates
`of that one point
dx#=x1#-x2#
dy#=y1#-y2#
if dx#>0
angle#=atan(dy#/dx#)+90
endif
if dx#<0
angle#=atan(dy#/dx#)+270
endif
if dx#=0
if dy#<0
angle#=0
endif
if dy#>0
angle#=180
endif
endif
`=================
endfunction angle#
`=================
`=====================================
function checkPtInSpr(spriteNum,x#,y#)
`=====================================
`Checks if a point's coordinates are within a specified sprite
flag=0
if x#<(sprite x(spriteNum)+sprite width(spriteNum)) and x#>sprite x(spriteNum) and y#<(sprite y(spriteNum)+sprite height(spriteNum)) and y#>sprite y(spriteNum)
flag=1
endif
`===============
endfunction flag
`===============
`===================================================================================================================
function bezierVal(x1 as double float,x2 as double float,x3 as double float,x4 as double float,time as double float)
`===================================================================================================================
`Finds the value of a number along a bezier curve at the specified time
a as double float
b as double float
c as double float
posX as double float
c = 3*(x2-x1)
b = 3*(x3-x2)-c
a = x4-x1-c-b
posX = a*time*time*time+b*time*time+c*time+x1
`===============
endfunction posX
`===============
`=============================================================================================================================================================================
function bezierLength(x1 as double float,x2 as double float,x3 as double float,x4 as double float,y1 as double float,y2 as double float,y3 as double float,y4 as double float)
`=============================================================================================================================================================================
`Finds the length of a bezier curve
length#=0
intervals=100
oldPosx#=0
oldPosy#=0
for t=1 to intervals
time#=t
intervals#=intervals
oldPosx#=bezierVal(x1,x2,x3,x4,(time#-1)/intervals#)
oldPosy#=bezierVal(y1,y2,y3,y4,(time#-1)/intervals#)
currentPosx#=bezierVal(x1,x2,x3,x4,time#/intervals#)
currentPosy#=bezierVal(y1,y2,y3,y4,time#/intervals#)
inc length#,getDistance(currentPosx#,currentPosy#,oldPosx#,oldPosy#)
next t
`==================
endfunction length#
`==================
GroupEngine-Groups objects, objects can be rotated around a center point and moved as if they were a single entity.
`[=======================]
` Group Engine
`[=======================]
`I. Description
` Contains a set of functions to move and rotate groups of objects.
`
`II. Functions
`
`Public:
` initializeGroupEngine()
` makeGroup()
` addMember()
` removeMember()
` positionGroup()
` rotateGroup()
` updateGroup()
` getGroupPopulation()
` freeGroup()
`
`Private Only:
` updateMember()
`===============================
function initializeGroupEngine()
`===============================
`Prepares the group engine by loading media and creating needed variables
`---------
`Variables
`---------
GLOBAL groupsMax=1
GLOBAL membersMax=1
`Array groups:
` x-group id y-member id
` groups(x,0)-the control object's id for the group
` groups(x,1...)-the member objects' id for the group
dim groups(groupsMax,membersMax)
`----------
`Load Media
`----------
load mesh "Models\cube.x",2000
`==========
endfunction
`==========
`====================================
function makeGroup(groupNum,x#,y#,z#)
`====================================
`Creates a new group by creating a new control object
if groupNum>groupsMax
dim groups(groupNum,membersMax)
groupsMax=groupNum
endif
groups(groupNum,0)=freeObject()
make object cube groups(groupNum,0),10
position object groups(groupNum,0),x#,y#,z#
hide object groups(groupNum,0)
`==========
endfunction
`==========
`=============================================================
function addMember(groupNum,objNum,xoffset#,yoffset#,zoffset#)
`=============================================================
`Makes a specified object a member of a group
controlObj=groups(groupNum,0)
groupx#=object position x(controlObj)
groupy#=object position y(controlObj)
groupz#=object position z(controlObj)
memberNum=freeLimb(controlObj)
if memberNum>membersMax
dim groups(groupsMax,memberNum)
membersMax=memberNum
endif
add limb controlObj,memberNum,2000
groups(groupNum,memberNum)=objNum
offset limb controlObj,memberNum,xoffset#,yoffset#,zoffset#
hide limb controlObj,memberNum
position object objNum,groupx#+xoffset#,groupy#+yoffset#,groupz#+zoffset#
`==========
endfunction
`==========
`=====================================
function removeMember(groupNum,objNum)
`=====================================
`Removes a member and the object associated with it from a group
controlObj=groups(groupNum,0)
x=-1
repeat
inc x
if groups(groupNum,x)=objNum then found=1
until found
groups(groupNum,x)=0
if getGroupPopulation(groupNum)=0
delete object controlObj
endif
`==========
endfunction
`==========
`========================================
function positionGroup(groupNum,x#,y#,z#)
`========================================
`Positions a group relative to its current position
controlObj=groups(groupNum,0)
objx#=object position x(controlObj)
objy#=object position y(controlObj)
objz#=object position z(controlObj)
position object controlObj,objx#+x#,objy#+y#,objz#+z#
`==========
endfunction
`==========
`=====================================================
function rotateGroup(groupNum,anglex#,angley#,anglez#)
`=====================================================
`Rotates a group relative to its current angle
controlObj=groups(groupNum,0)
objAx#=object angle x(controlObj)
objAy#=object angle y(controlObj)
objAz#=object angle z(controlObj)
rotate object controlObj,objAx#+anglex#,objAy#+angley#,objAz#+anglez#
`==========
endfunction
`==========
`=============================
function updateGroup(groupNum)
`=============================
`Updates the position and rotation of a group
controlObj=groups(groupNum,0)
x=0
while x<membersMax
inc x
if groups(groupNum,x)<>0
updateMember(groupNum,x)
endif
endwhile
`==========
endfunction
`==========
`========================================
function updateMember(groupNum,memberNum)
`========================================
`Updates an individual member in a group
controlObj=groups(groupNum,0)
objNum=groups(groupNum,memberNum)
memberx#=limb position x(controlObj,memberNum)
membery#=limb position y(controlObj,memberNum)
memberz#=limb position z(controlObj,memberNum)
memberAx#=limb angle x(controlObj,memberNum)
memberAy#=limb angle y(controlObj,memberNum)
memberAz#=limb angle z(controlObj,memberNum)
position object objNum,memberx#,membery#,memberz#
rotate object objNum,memberAx#,memberAy#,memberAz#
`==========
endfunction
`==========
`====================================
function getGroupPopulation(groupNum)
`====================================
`Returns the number of members in a group
controlObj=groups(groupNum,0)
population=0
x=0
while x<membersMax
inc x
if groups(groupNum,x)<>0
inc population
endif
endwhile
`=====================
endfunction population
`=====================
`===================
function freeGroup()
`===================
`Gets an unused group number
repeat
inc id
if object exist(groups(id,0))=0 then found=1
until found
`=============
endfunction id
`=============