3d Cloth Physics?
Blitz3D Forums/Blitz3D Programming/3d Cloth Physics?
| ||
| Does anyone have a sample or a rough guide on handling this? I suppose I could use a physics engine, but perhaps it's overkill just for a simple cloth? I'm also wondering about updating the mesh. Seems like vertexcoords can be pretty slow, but I can't really see an alternative. I suppose I could take a leaf from the book of the guy who wrote the water demo and prerender the animation, tile and animate only the reacting section. It might be convincing enough at a distance. It's pretty limiting though. |
| ||
| When I'm testing my code tend to be a mess, but may be this can help. What you see here is basically the relation the vertexes of a mesh should have between them to move as in cloth simulation, use arrows to move the system |
| ||
| Someone once posted a great example - was like a hanging flag cloth - was very good and quick - it doesn't appear to be in the code archives - From memory, they posted it into a thread - I haven't been able to find it. But it might prompt whoever did it to repost it. EDIT: I think I am homing in on it - might have good news - need a bit more time. EDIT: sniff - "andreas blixt" had a file called verlet3d.zip - However, it is not the one I was thinking of. |
| ||
| Verlet will do it just fine, the idea is to cause the position of each vertex to recieve and cause damping on about 8 of it's surrounding vertices. Verlet wouldn't even be needed, but would cover collisions(if you needed it to drape over things etc. |
| ||
| hmm, I'm not sure this is what you want, but its what I have on my drive, courtesy JFK: www.smartscreenuk.com/animatedflag.bb IPete2. |
| ||
| No, wasn't that one - the other one was a proper draping mesh that had weight to it and you could drag it and stuff - was very good. I reckon I have it on another PC as I tend to horde everything. |
| ||
| Oh Puki... please find it! -RZ |
| ||
| That one IIRC used a linepick from each vertex in the cloth mesh to properly drap over the mesh, unfortunately it wasnt fast enough (to many picks for a realtime application). |
| ||
| I FOUND IT!!!! Use your own texture in the example PS - WHO WROTE THIS CODE?
Graphics3D 640,480,16,2
ClearTextureFilters
Function interpolate#(v#,a#,b#,c#,d#)
Return c+(((v-a)*(d-c))/(b-a))
End Function
cam=CreateCamera()
PositionEntity cam,0,2,-20
CameraClsColor cam,100,100,100
fps=CreateTimer(30)
AmbientLight 50,50,50
l=CreateLight(2)
PositionEntity l,0,5,0
LightRange l,30
Const type_alf=1
Const type_c=2
;--------------------------------------------------------------
;------- PLANE -----------------------------------------------
grid=4
alf=CreateMesh()
EntityFX alf,16
EntityShininess alf,.75
EntityColor alf,100,100,50
; PUT YOUR TEXTURE IN HERE - added by puki - 30/3/05
tex=LoadTexture("yourtexture's name",1)
EntityTexture alf,tex
surf=CreateSurface(alf)
v_posx#=0
v_posz#=0
For g=0 To grid
v_posx=0
For g2=0 To grid
AddVertex(surf,v_posx,0,v_posz,v_posx/8,v_posz/8,0)
; c=CreateCube():PositionEntity c,v_posx,0,v_posz:ScaleEntity c,.2,.2,.2
v_posx=v_posx+2
v_index=v_index+1
Next
v_posz=v_posz+2
Next
For t=0 To 3
AddTriangle(surf,t,t+6,t+1)
AddTriangle(surf,t,t+6,t+5)
Next
For t=5 To 8
AddTriangle(surf,t,t+6,t+1)
AddTriangle(surf,t,t+6,t+5)
Next
For t=10 To 13
AddTriangle(surf,t,t+6,t+1)
AddTriangle(surf,t,t+6,t+5)
Next
For t=15 To 18
AddTriangle(surf,t,t+6,t+1)
AddTriangle(surf,t,t+6,t+5)
Next
For v=0 To CountVertices(surf)-1
VertexNormal surf,v,0,1,0
Next
Dim vertexes#(CountVertices(surf),3)
lock_vertexs=True
Dim v_collis(CountVertices(surf))
For v=0 To CountVertices(surf)-1
v_collis(v)=CreateSphere(4)
ScaleEntity v_collis(v),.15,.15,.15
EntityColor v_collis(v),0,255,0
EntityType v_collis(v),type_alf
EntityRadius v_collis(v),.15
Next
v_dumvecino=CreatePivot()
ball1=CreateSphere(8,v_dumvecino)
ScaleEntity ball1,.1,.1,.1
EntityColor ball1,255,0,0
EntityAlpha ball1,0
v_dumele=CreatePivot()
ball2=CreateSphere(8,v_dumele)
ScaleEntity ball2,.1,.1,.1
EntityColor ball2,0,255,0
EntityAlpha ball2,0
;-----------------------------------------------------------------------
;-----------------------------------------------------------------------
For c=1 To 1
cube=CreateCube()
PositionEntity cube,0,-10,0
ScaleEntity cube,5,0.5,5
; TurnEntity cube,0,Rand(-90,90),0
EntityType cube,type_c
Next
Collisions type_alf,type_c,2,2
While Not KeyDown(1)
WaitTimer(fps)
;s=GetSurface(test,1)
; If KeyHit(28)
; VertexCoords s,v,VertexX(s,v),VertexY(s,v)-2,VertexZ(s,v)
; v=v+1
; EndIf
; TurnEntity alf,0,0,1
PointEntity cam,v_dumele;alf
; MoveEntity cam,.1,0,0
If KeyDown(30)
vertexes(20,1)=vertexes(20,1)+.1
vertexes(24,1)=vertexes(24,1)+.1
vertexes(21,1)=vertexes(21,1)+.1
vertexes(23,1)=vertexes(23,1)+.1
EndIf
If KeyDown(31)
vertexes(20,1)=vertexes(20,1)-.1
vertexes(24,1)=vertexes(24,1)-.1
vertexes(21,1)=vertexes(21,1)-.1
vertexes(23,1)=vertexes(23,1)-.1
EndIf
If KeyDown(200)
vertexes(20,2)=vertexes(20,2)+.2
vertexes(24,2)=vertexes(24,2)+.2
vertexes(21,2)=vertexes(21,2)+.2
vertexes(23,2)=vertexes(23,2)+.2
EndIf
If KeyDown(208)
vertexes(20,2)=vertexes(20,2)-.2
vertexes(24,2)=vertexes(24,2)-.2
vertexes(21,2)=vertexes(21,2)-.2
vertexes(23,2)=vertexes(23,2)-.2
EndIf
If KeyDown(205)
vertexes(20,0)=vertexes(20,0)+.2
vertexes(24,0)=vertexes(24,0)+.2
vertexes(21,0)=vertexes(21,0)+.2
vertexes(23,0)=vertexes(23,0)+.2
EndIf
If KeyDown(203)
vertexes(20,0)=vertexes(20,0)-.2
vertexes(24,0)=vertexes(24,0)-.2
vertexes(21,0)=vertexes(21,0)-.2
vertexes(23,0)=vertexes(23,0)-.2
EndIf
If KeyHit(57) lock_vertexs=Not lock_vertexs
If KeyDown(28)
VertexCoords surf,24,EntityX(cube)+4,5,EntityZ(cube)
VertexCoords surf,23,EntityX(cube)+2,5,EntityZ(cube)
VertexCoords surf,21,EntityX(cube)-2,5,EntityZ(cube)
VertexCoords surf,20,EntityX(cube)-4,5,EntityZ(cube)
EndIf
x#=0
y#=0
z#=0
;**********************************
;--- change this parameters for testing ----
attract_force#=.65 ;better not above 1
separation_dist#=4 ;better not under 2
smooth=12
gravity#=.05;.075
;------------------------------------------------
;**********************************
For v=0 To CountVertices(surf)-1
Select v
Case 20
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
; PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
; PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
; SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
Case 24
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
; PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
; PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
; SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
Case 0
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
Case 1,2,3
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
Case 4
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
Case 5,10
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
Case 15
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
If Not lock_vertexs
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
EndIf
Case 9,14
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
Case 19
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
If Not lock_vertexs
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
EndIf
Case 22
; PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
; PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
; SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
; PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
; PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
; SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
Case 21
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
Case 23
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
Case 6,7,8,11,12,13,16,17,18
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-1),VertexY(surf,v-1),VertexZ(surf,v-1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-1,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v-5),VertexY(surf,v-5),VertexZ(surf,v-5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v-5,separation_dist,attract_force)
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+1),VertexY(surf,v+1),VertexZ(surf,v+1)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+1,separation_dist,attract_force)
If v<>16 And v<>18
PositionEntity v_dumele,VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
PositionEntity v_dumvecino,VertexX(surf,v+5),VertexY(surf,v+5),VertexZ(surf,v+5)
SET_VERTEX_VELOCITY(v_dumvecino,v_dumele,v+5,separation_dist,attract_force)
EndIf
End Select
Next
For v=0 To CountVertices(surf)-1
vertexes(v,0)=vertexes(v,0)+(-vertexes(v,0)/smooth)
vertexes(v,1)=vertexes(v,1)+(-vertexes(v,1)/smooth)
;Apply Gravity
If lock_vertexs=True
If v<>20 And v<>21 And v<>23 And v<>24
vertexes(v,1)=vertexes(v,1)-gravity
EndIf
Else
vertexes(v,1)=vertexes(v,1)-gravity
EndIf
vertexes(v,2)=vertexes(v,2)+(-vertexes(v,2)/smooth)
VertexCoords surf,v,VertexX(surf,v)+vertexes(v,0),VertexY(surf,v)+vertexes(v,1),VertexZ(surf,v)+vertexes(v,2)
; If VertexX(surf,v)>-5 And VertexX(surf,v)<5
; If VertexZ(surf,v)>-5 And VertexZ(surf,v)<5
If VertexY(surf,v)<-9
VertexCoords surf,v,VertexX(surf,v),-9,VertexZ(surf,v)
; vertexes(v,0)=0
; vertexes(v,1)=0
; vertexes(v,2)=0
EndIf
; EndIf
; EndIf
; If CountCollisions(v_collis(v))>0
; VertexCoords surf,v,EntityX(v_collis(v)),EntityY(v_collis(v)),EntityZ(v_collis(v))
; Else
; PositionEntity v_collis(v),VertexX(surf,v),VertexY(surf,v),VertexZ(surf,v)
; EndIf
Next
; For v=0 To CountVertices(surf)-1
; MoveEntity v_collis(v),(VertexX(surf,v)-EntityX(v_collis(v)))/20,(VertexY(surf,v)-EntityY(v_collis(v)))/20,(VertexZ(surf,v)-EntityZ(v_collis(v)))/20
; If CountCollisions(v_collis(v)) End
; Next
UpdateWorld
RenderWorld
; Text 40,40,TrisRendered()
; Text 40,60,vertexes(21,0)+" , "+vertexes(21,1)+" , "+vertexes(21,2)
Text 20,40,"-Use Arrows to Move"
Text 20,60,"-Use A and S to MoveUP and MoveDOWN"
Text 20,80,"-Use SPACE to toggle gravity"
Text 20,100,"-Use ENTER to restore to initial position"
Text 20,120,"-Use W to toggle Wireframe View"
If KeyHit(17) wire=Not wire WireFrame wire
Flip
Wend
Function SET_VERTEX_VELOCITY(vecino,ele,vertex,separation#,attract#=.2,newforceX#=0,newforceY#=0,newforceZ#=0)
dist#=EntityDistance(vecino,ele)
force#=interpolate(dist,0,separation,attract,-attract)
VectorX#=EntityX(vecino)-EntityX(ele)
VectorY#=EntityY(vecino)-EntityY(ele)
VectorZ#=EntityZ(vecino)-EntityZ(ele)
vectorX_U#=VectorX/dist
vectorY_U#=VectorY/dist
vectorZ_U#=VectorZ/dist
newforceX=newforceX+(force*vectorX_U)
newforceY=newforceY+(force*vectorY_U)
newforceZ=newforceZ+(force*vectorZ_U)
vertexes(vertex,0)=vertexes(vertex,0)+newforceX
vertexes(vertex,1)=vertexes(vertex,1)+newforceY
vertexes(vertex,2)=vertexes(vertex,2)+newforceZ
End Function
MoveMouse 400,300
|
| ||
| Jeez that example looks really complicated! Whats with the massive case statement? Take a look at the infamous 'Hitman Paper' on the net for some info on modelling cloth. From your original 'cloth' mesh create two list of types- points - these hold the x,y,z positions of your mesh vertices sticks - these link the points and are defined by their rest length - each timestep they modify the positions of the 2 points they connect in a direction perpendicular to the stick, so that the points end up the correct distance apart (stick length) at the end of each timestep. You'll have to update the whole mesh from these positions every timestep, but I'm not sure of the best way to do that- i think vertexcoords are your only option?? its late - may not have explained that well - but can give you some code demonstrating it in 2d if ur interested (in bmax tho). |
| ||
| @Puki, that code is mine :) it is basically the same code I first posted here, but applied in a odd (not optimized) way to each vertex in the plane... the big code has a dirty way to apply the velocities for the vertex but there's a function at the end of the code that could be useful for creating a generic code to apply "cloth movement" to any mesh... Paolo. |
| ||
| Paolo... that is some impressive coding! |
| ||
| Neat effect, my god! |