Hello Golwafael
I still had an old cloth physic codes from DPRo, which alters vertex within a mesh to simulate cloth.
I forgot who's the author though, sorry.
You will have to alter the commands it to make it compatible with AGK. Sorry I didn't convert it for you.
You can use AppGameKit memblock commands that alters vertexes within a mesh to replace the DBPro vertex-altering commands
Here's the flag physic demo. It's a cloth flag waving in the wind.
Put this before the main loop
REM MAKE CLOTH
#CONSTANT TRUE = 1
#CONSTANT FALSE = 0
type vector
x as float
y as float
z as float
endtype
rem declare variables for flag particles
type particle
mass as float
invMass as float
position as vector
velocity as vector
acceleration as vector
forces as vector
locked as boolean
endtype
type particleRef
r as integer
c as integer
endtype
type spring
p1 as particleRef
p2 as particleRef
k as float
d as float
L as float
endtype
type collision
p1 as particleRef
n as vector
endtype
Global NUMROWS = 8
Global NUMCOLS = 16
Global NUMSTRUCTURALSPRINGS
NUMSTRUCTURALSPRINGS = NUMCOLS*(NUMROWS+1) + NUMROWS*(NUMCOLS+1) + NUMCOLS*NUMROWS*2
NUMFACES = NUMROWS*NUMCOLS*2
Global CLOTHWIDTH = 100
Global CLOTHHEIGHT = 50
#CONSTANT CSTEP = CLOTHWIDTH / NUMCOLS
#CONSTANT RSTEP = CLOTHHEIGHT / NUMROWS
#CONSTANT NUMVERTICES = (NUMROWS+1) * (NUMCOLS+1)
dim particles(NUMROWS+1, NUMCOLS+1) as particle
dim structuralSprings(NUMSTRUCTURALSPRINGS) as spring
dim collisions(NUMVERTICES) as collision
Global windVector as vector
windVector.x = 10
windVector.y = 0
windVector.z = 1
Global GRAVITY# = -100.0
Global SPRINGTENSION# = 500.0
Global SPRINGSHEAR# = 300.0
Global SPRINGDAMPING# = 3.0
Global YOFFSET# = 0
Global DRAGCOEFFICIENT# = 0.01
Global WINDFACTOR# = 30.0
Global FLAGPOLEHEIGHT# = 200.0
Global FLAGPOLERADIUS# = 10.0
Global COLLISIONTOLERANCE# = 0.05
Global KRESTITUTION# = 0.25
Global FRICTIONFACTOR# = 0.5
GLOBAL CLOTHMASS# = 20.0
Global MASSPERFACE#
MASSPERFACE# = CLOTHMASS# / NUMFACES
initialize()
null = make vector3(11)
cloth1=1
buildFlag(cloth1, NUMCOLS, NUMROWS, CLOTHWIDTH, CLOTHHEIGHT)
texture object cloth1, 2
'xrotate object cloth1,270
'zrotate object cloth1,-60
particles(3,3).locked = TRUE
particles(3,4).locked = TRUE
Put this in the main loop
REM WIND FLAG
'position object cloth1,object position x(playerobj)+20,object position y(playerobj)+150,object position z(playerobj)+25
'position object cloth1,object position x(playerobj)+(cos(CameraAngleY#-80)*cos(CameraAngleX#)*20) ,object position y(playerobj)+150,object position z(playerobj)+(-sin(CameraAngleY#-80)*cos(CameraAnglex#)*20)
xrotate object cloth1, -(CurveAngle(CameraAngleY#,OldCamAngleY#,5))-90
if inkey$() = "x"
inc WINDFACTOR#, 1
endif
if inkey$() = "z"
dec WINDFACTOR#, 1
endif
rem 0.006
stepSimulation(0.006)
Put these after the main loop. these are the functions:
REM FUNCTIONS
Rem wait for the press of any key
Wait key
Rem then end
End
function showInfo()
print "Flag/Cloth simulation"
print "By: Phaelax"
print "Reference: Ch.17 of "Physics for Game Developers""
endfunction
function camera()
speed# = 5
cx# = camera position x()
cz# = camera position z()
a# = camera angle y()
cxa# = camera angle x()
if shiftkey() then speed# = 10
REMSTART
if upkey()=1
cx#=newxvalue(cx#,a#,speed#)
cz#=newzvalue(cz#,a#,speed#)
endif
if downkey()=1
cx#=newxvalue(cx#,a#,-speed#)
cz#=newzvalue(cz#,a#,-speed#)
endif
if leftkey()=1
cx#=newxvalue(cx#,wrapvalue(a#-90.0),speed#)
cz#=newzvalue(cz#,wrapvalue(a#-90.0),speed#)
endif
if rightkey()=1
cx#=newxvalue(cx#,wrapvalue(a#+90.0),speed#)
cz#=newzvalue(cz#,wrapvalue(a#+90.0),speed#)
endif
REMEND
a#=wrapvalue(a#+(mousemovex()/3.0))
cxa#=cxa#+(mousemovey()/3.0)
if cxa#<-90.0 then cxa#=-90.0
if cxa#>90.0 then cxa#=90.0
position camera cx#,100,cz#
rotate camera cxa#,a#,0
endfunction
function buildFlag(obj as integer, xsegs as float, zsegs as float, width as float, height as float)
mesh = 1
mem = 1
header = 12
size = 36
vertCount = xsegs*zsegs*6
totalSize = vertCount*size + header
tx# = width/xsegs
tz# = height/zsegs
make memblock mem, totalSize
write memblock dword mem, 0, 338
write memblock dword mem, 4, size
write memblock dword mem, 8, vertCount
tile = 0
for z = 1 to zsegs
for x = 1 to xsegs
location = tile*size*6 + header
U1# = (x-1)/xsegs
V1# = (z-1)/zsegs
U2# = x/xsegs
V2# = z/zsegs
x1# = particles(z,x-1).position.x
y1# = particles(z,x-1).position.y
z1# = particles(z,x-1).position.z
x2# = particles(z-1,x-1).position.x
y2# = particles(z-1,x-1).position.y
z2# = particles(z-1,x-1).position.z
x3# = particles(z-1,x).position.x
y3# = particles(z-1,x).position.y
z3# = particles(z-1,x).position.z
x4# = x1#
y4# = y1#
z4# = z1#
x5# = x3#
y5# = y3#
z5# = z3#
x6# = particles(z,x).position.x
y6# = particles(z,x).position.y
z6# = particles(z,x).position.z
rem vert 1 - x,y,z, normals x,y,z, diffuse color
write memblock float mem, location, x1#
write memblock float mem, location+4, y1#
write memblock float mem, location+8, z1#
write memblock float mem, location+28, U1#
write memblock float mem, location+32, V2#
rem vert 2 - x,y,z
temp_size = size
write memblock float mem, location+temp_size, x2#
write memblock float mem, location+temp_size+4, y2#
write memblock float mem, location+temp_size+8, z2#
write memblock float mem, location+temp_size+28, U1#
write memblock float mem, location+temp_size+32, V1#
rem vert 3 - x,y,z
temp_size = size*2
write memblock float mem, location+temp_size, x3#
write memblock float mem, location+temp_size+4, y3#
write memblock float mem, location+temp_size+8, z3#
write memblock float mem, location+temp_size+28, U2#
write memblock float mem, location+temp_size+32, V1#
rem vert 4 - x,y,z
temp_size = size*3
write memblock float mem, location+temp_size, x4#
write memblock float mem, location+temp_size+4, y4#
write memblock float mem, location+temp_size+8, z4#
write memblock float mem, location+temp_size+28, U1#
write memblock float mem, location+temp_size+32, V2#
rem vert 5 - x,y,z
temp_size = size*4
write memblock float mem, location+temp_size, x5#
write memblock float mem, location+temp_size+4, y5#
write memblock float mem, location+temp_size+8, z5#
write memblock float mem, location+temp_size+28, U2#
write memblock float mem, location+temp_size+32, V1#
rem vert 6 - x,y,z
temp_size = size*5
write memblock float mem, location+temp_size, x6#
write memblock float mem, location+temp_size+4, y6#
write memblock float mem, location+temp_size+8, z6#
write memblock float mem, location+temp_size+28, U2#
write memblock float mem, location+temp_size+32, V2#
inc tile, 1
next x
next z
make mesh from memblock mesh, mem
make object obj,mesh,0
set object cull obj, 0
set object ambient obj, 0
endfunction
function calcForces()
null = make vector3(1)
null = make vector3(2)
null = make vector3(3)
null = make vector3(4)
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
particles(r,c).forces.x = 0
particles(r,c).forces.y = 0
particles(r,c).forces.z = 0
next c
next r
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
if particles(r,c).locked = FALSE
rem gravity
particles(r,c).forces.y = particles(r,c).forces.y + (GRAVITY#*particles(r,c).mass)
rem viscous drag
set vector3 1, -particles(r,c).velocity.x, -particles(r,c).velocity.y, -particles(r,c).velocity.z
normalize vector3 1,1
multiply vector3 1, length vector3(1)^2 * DRAGCOEFFICIENT#
set vector3 2, particles(r,c).forces.x, particles(r,c).forces.y, particles(r,c).forces.z
add vector3 1, 1, 2
particles(r,c).forces.x = x vector3(1)
particles(r,c).forces.y = y vector3(1)
particles(r,c).forces.z = z vector3(1)
rem wind
set vector3 1, rnd(7)+5,0,rnd(3)-1
normalize vector3 1, 1
set vector3 2, particles(r,c).forces.x, particles(r,c).forces.y, particles(r,c).forces.z
multiply vector3 1, rnd(WINDFACTOR#)
add vector3 1, 2, 1
particles(r,c).forces.x = x vector3(1)
particles(r,c).forces.y = y vector3(1)
particles(r,c).forces.z = z vector3(1)
endif
next c
next r
rem process spring forces
for i = 0 to NUMSTRUCTURALSPRINGS-1
r1 = structuralSprings(i).p1.r
c1 = structuralSprings(i).p1.c
r2 = structuralSprings(i).p2.r
c2 = structuralSprings(i).p2.c
set vector3 1, particles(r1,c1).position.x, particles(r1,c1).position.y, particles(r1,c1).position.z
set vector3 2, particles(r2,c2).position.x, particles(r2,c2).position.y, particles(r2,c2).position.z
rem d
subtract vector3 3, 1, 2
set vector3 1, particles(r1,c1).velocity.x, particles(r1,c1).velocity.y, particles(r1,c1).velocity.z
set vector3 2, particles(r2,c2).velocity.x, particles(r2,c2).velocity.y, particles(r2,c2).velocity.z
rem v
subtract vector3 4, 1, 2
copy vector3 1, 3
copy vector3 2, 3
L# = structuralSprings(i).L
f1# = -(structuralSprings(i).k * (length vector3(3)-L#) + structuralSprings(i).d * (dot product vector3(4,3) / length vector3(3)))
divide vector3 3, length vector3(3)
multiply vector3 3, f1#
set vector3 4, x vector3(3), y vector3(3), z vector3(3)
multiply vector3 4, -1
if particles(r1,c1).locked = FALSE
particles(r1,c1).forces.x = particles(r1,c1).forces.x + x vector3(3)
particles(r1,c1).forces.y = particles(r1,c1).forces.y + y vector3(3)
particles(r1,c1).forces.z = particles(r1,c1).forces.z + z vector3(3)
endif
if particles(r2,c2).locked = FALSE
particles(r2,c2).forces.x = particles(r2,c2).forces.x + x vector3(4)
particles(r2,c2).forces.y = particles(r2,c2).forces.y + y vector3(4)
particles(r2,c2).forces.z = particles(r2,c2).forces.z + z vector3(4)
endif
next i
null = delete vector3(1)
null = delete vector3(2)
null = delete vector3(3)
null = delete vector3(4)
endfunction
rem remove the inner IF statement to unlock ALL anchored points
function unlockAll()
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
if r = 0 and c = 0
else
particles(r,c).locked = FALSE
endif
next c
next r
endfunction
function initialize()
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
rem calculate mass of this particle
if (r = 0) and (c = 0)
f = 1
else
if (r = NUMROWS) and (c = 0)
f = 2
else
if (r = 0) and (c = NUMCOLS)
f = 2
else
if (r = NUMROWS) and (c = NUMCOLS)
f = 1
else
if ((r = 0) or (r = NUMROWS)) and ((c <> 0) and (c <> NUMCOLS))
f = 3
else
f = 6
endif
endif
endif
endif
endif
particles(r,c).mass = (f * MASSPERFACE#) / 3
particles(r,c).invMass = 1 / particles(r,c).mass
rem set initial position of this particle
particles(r,c).position.x = c * CSTEP
particles(r,c).position.y = (CLOTHHEIGHT - (r * RSTEP)) + YOFFSET
particles(r,c).position.z = 0
rem set initial velocity and forces to zero
particles(r,c).velocity.x = 0
particles(r,c).velocity.y = 0
particles(r,c).velocity.z = 0
particles(r,c).acceleration.x = 0
particles(r,c).acceleration.y = 0
particles(r,c).acceleration.z = 0
particles(r,c).forces.x = 0
particles(r,c).forces.y = 0
particles(r,c).forces.z = 0
if (c = 0) and (r = 0 or r = NUMROWS)
particles(r,c).locked = TRUE
else
particles(r,c).locked = FALSE
endif
next c
next r
null = make vector3(1)
null = make vector3(2)
count = 0
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
rem setup the structural springs
if (c < NUMCOLS)
structuralSprings(count).p1.r = r
structuralSprings(count).p1.c = c
structuralSprings(count).p2.r = r
structuralSprings(count).p2.c = c+1
structuralSprings(count).k = SPRINGTENSION#
structuralSprings(count).d = SPRINGDAMPING#
set vector3 1, particles(r,c).position.x, particles(r,c).position.y, particles(r,c).position.z
set vector3 2, particles(r,c+1).position.x, particles(r,c+1).position.y, particles(r,c+1).position.z
subtract vector3 1,1,2
structuralSprings(count).L = length vector3(1)
inc count, 1
endif
if (r < NUMROWS)
structuralSprings(count).p1.r = r
structuralSprings(count).p1.c = c
structuralSprings(count).p2.r = r+1
structuralSprings(count).p2.c = c
structuralSprings(count).k = SPRINGTENSION#
structuralSprings(count).d = SPRINGDAMPING#
set vector3 1, particles(r,c).position.x, particles(r,c).position.y, particles(r,c).position.z
set vector3 2, particles(r+1,c).position.x, particles(r+1,c).position.y, particles(r+1,c).position.z
subtract vector3 1,1,2
structuralSprings(count).L = length vector3(1)
inc count, 1
endif
if (r < NUMROWS) and (c < NUMCOLS)
structuralSprings(count).p1.r = r
structuralSprings(count).p1.c = c
structuralSprings(count).p2.r = r+1
structuralSprings(count).p2.c = c+1
structuralSprings(count).k = SPRINGTENSION#
structuralSprings(count).d = SPRINGDAMPING#
set vector3 1, particles(r,c).position.x, particles(r,c).position.y, particles(r,c).position.z
set vector3 2, particles(r+1,c+1).position.x, particles(r+1,c+1).position.y, particles(r+1,c+1).position.z
subtract vector3 1,1,2
structuralSprings(count).L = length vector3(1)
inc count, 1
endif
if (c > 0) and (r < NUMROWS)
structuralSprings(count).p1.r = r
structuralSprings(count).p1.c = c
structuralSprings(count).p2.r = r+1
structuralSprings(count).p2.c = c-1
structuralSprings(count).k = SPRINGTENSION#
structuralSprings(count).d = SPRINGDAMPING#
set vector3 1, particles(r,c).position.x, particles(r,c).position.y, particles(r,c).position.z
set vector3 2, particles(r+1,c-1).position.x, particles(r+1,c-1).position.y, particles(r+1,c-1).position.z
subtract vector3 1,1,2
structuralSprings(count).L = length vector3(1)
inc count, 1
endif
next c
next r
null = delete vector3(1)
null = delete vector3(2)
endfunction
function resolveCollisions()
null = make vector3(1)
null = make vector3(2)
null = make vector3(3)
for i = 0 to NUMVERTICES-1
if collisions(i).p1.r <> -1
r = collisions(i).p1.r
c = collisions(i).p1.c
set vector3 1, collisions(i).n.x, collisions(i).n.y, collisions(i).n.z
set vector3 2, particles(r,c).velocity.x, particles(r,c).velocity.y, particles(r,c).velocity.z
copy vector3 3, 1
multiply vector3 3,dot product vector3(1,2)
set vector3 1, particles(r,c).velocity.x, particles(r,c).velocity.y, particles(r,c).velocity.z
subtract vector3 1, 1, 3
multiply vector3 3,-(KRESTITUTION#+1)
multiply vector3 1, FRICTIONFACTOR#
add vector3 1, 3, 1
particles(r,c).velocity.x = x vector3(1)
particles(r,c).velocity.y = y vector3(1)
particles(r,c).velocity.z = z vector3(1)
endif
next i
endfunction
function updateClothGeometry()
lock VERTEXDATA FOR LIMB 1, 0
vert = 0
for z = 1 to NUMROWS
for x = 1 to NUMCOLS
x1# = particles(z,x-1).position.x
y1# = particles(z,x-1).position.y
z1# = particles(z,x-1).position.z
x2# = particles(z-1,x-1).position.x
y2# = particles(z-1,x-1).position.y
z2# = particles(z-1,x-1).position.z
x3# = particles(z-1,x).position.x
y3# = particles(z-1,x).position.y
z3# = particles(z-1,x).position.z
x4# = x1#
y4# = y1#
z4# = z1#
x5# = x3#
y5# = y3#
z5# = z3#
x6# = particles(z,x).position.x
y6# = particles(z,x).position.y
z6# = particles(z,x).position.z
`VB SET VERTEX DATA POSITION vert, particles(z,x).position.x, particles(z,x).position.y, particles(z,x).position.z
SET VERTEXDATA POSITION vert, x1#, y1#, z1#
SET VERTEXDATA POSITION vert+1, x2#, y2#, z2#
SET VERTEXDATA POSITION vert+2, x3#, y3#, z3#
SET VERTEXDATA POSITION vert+3, x4#, y4#, z4#
SET VERTEXDATA POSITION vert+4, x5#, y5#, z5#
SET VERTEXDATA POSITION vert+5, x6#, y6#, z6#
inc vert, 6
next x
next z
UNLOCK VERTEXDATA
endfunction
function stepSimulation(dt as float)
rem calculate all the forces
calcForces()
rem integrate
for r = 0 to NUMROWS
for c = 0 to NUMCOLS
set vector3 11, particles(r,c).forces.x,particles(r,c).forces.y,particles(r,c).forces.z
multiply vector3 11, particles(r,c).invMass
particles(r,c).acceleration.x = x vector3(11)
particles(r,c).acceleration.y = y vector3(11)
particles(r,c).acceleration.z = z vector3(11)
multiply vector3 11, dt
particles(r,c).velocity.x = particles(r,c).velocity.x + x vector3(11)
particles(r,c).velocity.y = particles(r,c).velocity.y + y vector3(11)
particles(r,c).velocity.z = particles(r,c).velocity.z + z vector3(11)
particles(r,c).position.x = particles(r,c).position.x + particles(r,c).velocity.x * dt
particles(r,c).position.y = particles(r,c).position.y + particles(r,c).velocity.y * dt
particles(r,c).position.z = particles(r,c).position.z + particles(r,c).velocity.z * dt
next c
next r
rem check for collision
`check = checkForCollision()
`if check = TRUE then resolveCollisions()
updateClothGeometry()
endfunction
WOops just realized that AppGameKit does not have commands to unlock/lock vertex data directly.
So probably had to create/recreate the mesh-memblock every frame, which sounds grim
Tough luck. sorry