TGC Codebase Backup



Simple object deformation using an object by Narf The Mouse

4th May 2005 4:27
Summary

Squish one object with another.



Description

This code will hopefully help with such things as making sure your characters' hair doesn't appear outside the characters' helmet/hat/whatever.

It contains two example codes, one which is direction-oriented for a single plain and one which is uni-directional. It uses the new VertexData commands



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on : sync rate 0 : hide mouse : randomize timer( )

make object sphere 1, 50 ` The object to be deformed. The one below this will do the deforming
make object plain 2, 50, 50 : position object 2, 50, 0, 0 : rotate object 2, 0, 90, 0
Marker = 3 : make object sphere Marker, 5 : hide object Marker ` A marker object

LOCK VERTEXDATA FOR LIMB 1, 0 : n=GET VERTEXDATA VERTEX COUNT( ) ` So we can move vertices
global dim OVertexPos#( n, 2 ) ` Get all the vertices original vertex data
for t = 0 to n
OVertexPos#( t, 0 ) = GET VERTEXDATA POSITION X( t )
OVertexPos#( t, 1 ) = GET VERTEXDATA POSITION Y( t )
OVertexPos#( t, 2 ) = GET VERTEXDATA POSITION Z( t )
next t
x# = object position x( 1 ) : y# = object position y( 1 ) : z# = object position z( 1 )
px# = 100 ` Get object #1's centerpoint above; this line is the initial position of object #2

do
for t = 0 to n ` Go through all the vertices
x2# = GET VERTEXDATA POSITION X( t ) ` Get the vertex position
y2# = GET VERTEXDATA POSITION Y( t )
z2# = GET VERTEXDATA POSITION Z( t )

x3# = OVertexPos#( t, 0 ) : y3# = OVertexPos#( t, 1 ) : z3# = OVertexPos#( t, 2 )
x4# = x3# - x2# : y4# = y3# - y2# : z4# = z3# - z2# ` This tries to move all the vertices
x2# = x2# + ( x4# * 0.01 ) : y2# = y2# + ( y4# * 0.01 ) : z2# = z2# + ( z4# * 0.01 )
SET VERTEXDATA POSITION t, x2#, y2#, z2# ` back to their original positions

position object Marker, x2#, y2#, z2#
rotate object Marker, object angle x( 2 ), object angle y( 2 ), object angle z( 2 )
move object Marker, -20
xb# = object position x( 3 ) : yb# = object position y( 3 ) : zb# = object position z( 3 )
d# = intersect object( 2, xb#, yb#, zb#, x2#, y2#, z2# )
` The above code is a simple way of checking wether the vertex is being affected by the plain.
` It checks against the anlge of the plain. At the bottom is code that will check from the
` middle of the object.

if d# <> 0 ` If the vertex is behind the plain
move object Marker, d#
SET VERTEXDATA POSITION t, object position x( Marker ), object position y( Marker ), object position z( Marker )
` That repositions the vertex onto the plain. :)
endif
next t
rem cy# = wrapvalue( cy# + 0.1 ) ` Code to rotate the camera
position camera px#, 0, 0
rotate camera 0, cy#, 0
move camera 0, -200
pxm# = mousemovex( ) : pxm# = pxm# * 0.156 : px# = px# + pxm#
position object 2, px#, 0, 0 ` So we can move the plain with the mouse.
sync
loop

 ` This code has the advantage of operating independant
 ` of the object that's doing the squishing. It also
 ` squishes towards the center - But for helmets and hair
 ` nobody's going to see, anyway. :)
remstart
if d# <> 0
x3# = abs( x2# - x# ) : y3# = abs( y2# - y# ) : z3# = abs( z2# - z# )
tc# = ( x3#^2 + y3#^2 + z3#^2 ) ^ 0.5 ` Get the distance between the center and the vertex
x4# = x3# / tc# : y4# = y3# / tc# : z4# = z3# / tc#
x5# = x4# * d# : y5# = y4# * d# : z5# = z4# * d# ` And squish
if x2# < 0 then x5# = x5# * -1
if y2# < 0 then y5# = y5# * -1
if z2# < 0 then z5# = z5# * -1
SET VERTEXDATA POSITION t, x5#, y5#, z5#
endif
remend