Rewrote my model "building" function today and decided to share it with y'all.
In my project, models are constructed from individual objects which allows for, among other things, easy targeting and destruction of individual parts of tanks and other models.
I've simplified the process so that most parameters for constructing models within my program are set during modeling of the objects. I first build the entire model then split it up into its component parts. Along with this, I add "mount to" points to parent objects and "mount from" points to child objects. Each mount point also has X, Y, and Z mount direction points associated with it.
When it comes time to load a complete model, the program runs through a configuration script to load the individual objects and bind them to the correct mount points (another function, which I might post in the future). The program calculates the proper orientation for child objects without any input from the script, using joint data contained in each object.
Anywho, the program snippet is posted and included, along with a simple two object model for your enjoyment. The program is hard-coded to lead the objects from C:\, change as needed. The included objects are for example use only, not your own use or otherwise.
sync on
sync rate 30
a = make vector3(1)
a = make vector3(2)
a = make vector3(3)
a = make vector3(4)
a = make vector3(5)
a = make vector3(6)
a = make vector3(7)
a = make vector3(8)
load object "c:\bmn-1a_CT.x", 1
color object 1, rgb(255, 255, 255)
empty checklist
perform checklist for object limbs 1
for a = 1 to checklist quantity()
if limb name$(1, a - 1) = "Mount_Cockpit"
MavCockpit = a - 1
endif
if limb name$(1, a - 1) = "Dir_X_Cockpit"
MavCockpitX = a - 1
endif
if limb name$(1, a - 1) = "Dir_Y_Cockpit"
MavCockpitY = a - 1
endif
if limb name$(1, a - 1) = "Dir_Z_Cockpit"
MavCockpitZ = a - 1
endif
next a
autocam off
load object "c:\cockpit.x", 2
color object 2, rgb(255, 255,255)
empty checklist
perform checklist for object limbs 2
for a = 1 to checklist quantity()
if limb name$(2, a - 1) = "Mass"
CockpitMass = a - 1
endif
if limb name$(2, a - 1) = "Mount_From"
Cockpit = a - 1
endif
if limb name$(2, a - 1) = "Dir_X_Mount_From"
CockpitX = a - 1
endif
if limb name$(2, a - 1) = "Dir_Y_Mount_From"
CockpitY = a - 1
endif
if limb name$(2, a - 1) = "Dir_Z_Mount_From"
CockpitZ = a - 1
endif
next a
do
a# = a# + 0.5
b# = b# + 0.25
c# = c# + 0.125
rotate object 1, a#, b#, c#
`Place the origin of the X, Y and Z direction joints of the parent object at 0, 0, 0 by subtracting their positions from the "mount to" joint's position
set vector3 7, limb position x(1, MavCockpit), limb position y(1, MavCockpit), limb position z(1, MavCockpit)
set vector3 1, limb position x(1, MavCockpitX), limb position y(1, MavCockpitX), limb position z(1, MavCockpitX)
set vector3 2, limb position x(1, MavCockpitY), limb position y(1, MavCockpitY), limb position z(1, MavCockpitY)
set vector3 3, limb position x(1, MavCockpitZ), limb position y(1, MavCockpitZ), limb position z(1, MavCockpitZ)
subtract vector3 1, 7, 1
subtract vector3 2, 7, 2
subtract vector3 3, 7, 3
`Place the origin of the X, Y and Z direction joints of the child object at 0, 0, 0 by subtracting their positions from the "mount from" joint's position
set vector3 8, limb position x(2, Cockpit), limb position y(2, Cockpit), limb position z(2, Cockpit)
set vector3 4, limb position x(2, CockpitX), limb position y(2, CockpitX), limb position z(2, CockpitX)
set vector3 5, limb position x(2, CockpitY), limb position y(2, CockpitY), limb position z(2, CockpitY)
set vector3 6, limb position x(2, CockpitZ), limb position y(2, CockpitZ), limb position z(2, CockpitZ)
subtract vector3 4, 8, 4
subtract vector3 5, 8, 5
subtract vector3 6, 8, 6
`The vectors need to be normalized to calculate the required angles
normalize vector3 1, 1
normalize vector3 2, 2
normalize vector3 3, 3
normalize vector3 4, 4
normalize vector3 5, 5
normalize vector3 6, 6
`Calculate the angle diffeerence between the parent "mount to" joint and the child "mount from" joint
x# = acos(dot product vector3(1, 4))
y# = acos(dot product vector3(2, 5))
z# = acos(dot product vector3(3, 6))
`Rotate the object as needed to orient it properly to the parent "mount from" joint
rotate object 2, a#,b#,c#
`Calculate the position of the child "mount from" joint relative to the object's mass joint
set vector3 4, limb position x(2, CockpitMass), limb position y(2, CockpitMass), limb position z(2, CockpitMass)
set vector3 5, limb position x(2, Cockpit), limb position y(2, Cockpit), limb position z(2, Cockpit)
subtract vector3 4, 4, 5
`Place the child object at the parent object's "mount from" joint
position object 2, x vector3(4) + x vector3(7) , y vector3(4) + y vector3(7), z vector3(4) + z vector3(7)
control camera using arrowkeys 0, 1, 1
sync
loop
Comments and suggestions are welcome...enjoy!
In Development: K96 - Combat Simulation