MP3D Engine Alpha 33

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MP3D Engine Alpha 32

Post by applePi »

Psychophanta , i depends on one of the mesh creations example in PB-Ogre. it is good that Michael have made his Graphics engine in parallel with PB ogre, except of course the physics engine
only the texturing needs corrections, may be every shape have its specifics, but for now i consider the u, v to go from 1 to 4*pi. regarding the physics we can insert a physical ball inside the torus only if its physics body have a property as static
Image

Psychophanta example ported to PB-Ogre

Code: Select all

Enumeration
  #camera
  #sphere
EndEnumeration  
#CameraSpeed = 0.5
Global torus
Define.f MouseX, MouseY, keyX, keyY
InitEngine3D()

Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)

InitSprite()
InitKeyboard()
InitMouse()
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)

OpenWindow(0, 0, 0, DesktopW, DesktopH, "Torus arc Mesh .....'W' for wire/solid frame ..... mouse /arrow keys to move/rotate the camera ")
OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

CreateCamera(#camera, 0, 0, 100, 100)
MoveCamera(#camera, 0, 5, 10, #PB_Absolute)
CameraLookAt(#camera,0,0,0)
CameraBackColor(#camera, RGB(0,0,0))

CreateLight(0,RGB(255,255,255),10,20,0)
AmbientColor(RGB(200,200,200))

CreateMaterial(1, LoadTexture(1, "ground_diffuse.png")) ; Geebee2.bmp ; MRAMOR6X6.jpg; ground_diffuse.png
MaterialCullingMode(1, #PB_Material_NoCulling)
MaterialShadingMode(1, #PB_Material_Wireframe)
CreateMaterial(2, LoadTexture(2, "Geebee2.bmp"))
MaterialCullingMode(2, #PB_Material_NoCulling)

Structure Point3D
  x.f:y.f:z.f
EndStructure 
Structure Vector3D Extends Point3D
  m.f;<-length(modulo)
EndStructure
Macro Wrap(number,margin1,margin2)
  (margin1#+(number#)%((margin2#)-(margin1#)))
EndMacro
Macro ProductoEscalar(a,b)
  (a#\x*b#\x+a#\y*b#\y+a#\z*b#\z)
EndMacro
Macro getmodulo(v)
  (Sqr#ProductoEscalar(v#,v#))
EndMacro
Procedure Rotate_3DVector_by_Angle_adding(*fi.Vector3D,*R0.Vector3D)
  ;Esta funcion halla un vector resultado de una rotacion en el espacio de otro vector inicial.
  ;Esta funcion admite entonces, como parametros de entrada, 2 vectores:
  ; - 'Vector radio' que se desea rotar. Este vector tiene direccion no colineal con el eje de rotaci?n.
  ; - 'Vector angulo' ('velocidad angular' * 'tiempo'), es el angulo en el que se rota el 'Vector radio' dado.
  ;     Su modulo indica el numero de radianes a rotar, su direccion indica el angulo en el espacio en el que se rota (eje de rotaci?n)
  ;     su sentido indica el sentido de la rotacion
  ;NOTA: la funcion devuelve el vector velocidad rectilinea (3er parametro) y el nuevo vector radio (4? parametro)
  ;       pero ambos parametros se pueden omitir haciendo que la velocidad rectilinea no se devuelva y que el nuevo radio se devuelva en el 2? parametro
  Protected Rt.Vector3D,u.Vector3D,P0.Vector3D
  *fi\m=ProductoEscalar(*fi,*fi)
  If *fi\m
    u\m=ProductoEscalar(*R0,*fi)/*fi\m
    *fi\m=Sqr(*fi\m)
    ;Proyeccion de *R0 sobre *fi:
    Rt\x=u\m**fi\x
    Rt\y=u\m**fi\y
    Rt\z=u\m**fi\z
    ;Calcular P0-> (proyeccion ortogonal de *R0 sobre *fi):
    P0\x=*R0\x-Rt\x
    P0\y=*R0\y-Rt\y
    P0\z=*R0\z-Rt\z
    P0\m=getmodulo(P0)
    If P0\m=0.0:ProcedureReturn:EndIf
    ;Calcular el producto vectorial: u-> = *fi-> X P0->
    u\x=*fi\y*P0\z-*fi\z*P0\y
    u\y=*fi\z*P0\x-*fi\x*P0\z
    u\z=*fi\x*P0\y-*fi\y*P0\x
    ;ahora obtener *R0-> = (Proyeccion de *R0 sobre *fi)-> + (cos(|*fi->|)·P0-> + |P0->|/|u->|·sin(|*fi->|)·u->)->:
    u\m=getmodulo(u)
    *R0\x=Rt\x
    *R0\y=Rt\y
    *R0\z=Rt\z
    !mov edi,dword[p.p_fi]
    !fld dword[edi+12]; <- get *fi\m
    !fsincos
    !fstp dword[p.v_Rt]; <- Rt\x=Cos(*fi\m)
    !fstp dword[p.v_Rt+4]; <- Rt\y=Sin(*fi\m)
    ; o bien si no se quiere usar ensamblador sustituirlo por
    ; Rt\x=Cos(*fi\m):Rt\y=Sin(*fi\m)
    *R0\x+Rt\x*P0\x+P0\m/u\m*Rt\y*u\x
    *R0\y+Rt\x*P0\y+P0\m/u\m*Rt\y*u\y
    *R0\z+Rt\x*P0\z+P0\m/u\m*Rt\y*u\z
    *R0\m=P0\m
  EndIf
EndProcedure
Procedure.i CreateTorusArc(l.l,lt.l,r0.f,r1.f,rt.f,at.f,t.f=0.0)
  txu.f : txv.f
  If l<2 Or lt<2 Or r0<0 Or r1<0 Or rt<0.0001:ProcedureReturn 0:EndIf
  Protected vert.Vector3D,ang.Vector3D,a.l,b.l,p.f,torsion.f,v0.l,v1.l
  ;Mesh.i=MP_CreateMesh()
  CreateMesh(3 , #PB_Mesh_TriangleList, #PB_Mesh_Dynamic )
  
  ;l = numero de lados en la seccion (number of sides in cut area)
  ;lt = lados toro (number of sides of torus)
  ;r0 = radio grosor inicial (radius of cut area at initial extreme)
  ;r1 = radio grosor final (radius of cut area at end extreme)
  ;rt = radio del toro (radius of torus)
  ;at = longitud del arco del toro (arc lenght of the torus)
  ;t = torsion (torsion)
  For b=0 To lt
    For a=0 To l-1
      p=r0+(r1-r0)*b/lt
      vert\x=rt+p*Cos(2*#PI*a/l+torsion) +(r1-r0)*b/lt/2
      vert\y=p*Sin(2*#PI*a/l+torsion)
      vert\z=0
      If b<>0
        ang\x=0:ang\y=-at*b/lt:ang\z=0
        Rotate_3DVector_by_Angle_adding(@ang,@vert)
      EndIf
      ;MP_AddVertex(Mesh.i,vert\x,vert\y,vert\z,0,Int(a/l)/(lt+1),Mod(a,l)/l)
      MeshVertexPosition(vert\x,vert\y,vert\z)
      MeshVertexTextureCoordinate(txu, txv)
      ;MeshVertexTextureCoordinate(Int(a/l)/(lt+1),Mod(a,l)/l)
      MeshVertexNormal(vert\x,vert\y,vert\z)
      ;MeshVertexColor(RGB(255,255,0))
      txv = txv + 1/(4*#PI)

    Next
    torsion+t
    txv = 0
    txu = txu + 1/(4*#PI)   ; texture coordinates
    
  Next
  For b=0 To lt-1
    For a=0 To l-1
      v0=Wrap(b*l+a,b*l,b*l+l):v1=Wrap(b*l+a+l+1,b*l+l,b*l+2*l); <- uso estas variables para no recalcularlas (para ganar tiempo)
      ;MP_AddTriangle(Mesh.i,v0,Wrap(b*l+a+l,b*l+l,b*l+2*l),v1)
      MeshFace(v0,Wrap(b*l+a+l,b*l+l,b*l+2*l),v1)
      ;MP_AddTriangle(Mesh.i,v0,Wrap(b*l+a+1,b*l,b*l+l),v1)
      MeshFace(v0,Wrap(b*l+a+1,b*l,b*l+l),v1)
    Next
  Next
  
  NormalizeMesh(1)
  FinishMesh(#True)
  CreateEntity(3,MeshID(3),MaterialID(1))
  ;MP_EntitySetNormals(Mesh.i)
  ;ProcedureReturn Mesh.i
EndProcedure
;Tor.i=CreateTorusArc(25,3,0.4,0,2,#PI,0)
;Tor.i=CreateTorusArc(25,5,0.4,0.2,2,#PI,0)
; Tor.i=CreateTorusArc(3,30,1,0,2,#PI/3,0)
; Tor.i=CreateTorusArc(16,30,0.4,0.4,2,#PI,0)
;Tor.i=CreateTorusArc(4,10,1,0.5,2,#PI/2,0.1)
;Tor.i=CreateTorusArc(3,2,1,1,2,#PI/8,0)
Tor.i=CreateTorusArc(16,30,1,0,2,2.2*#PI,0)
;Tor.i=CreateTorusArc(16,30,1, 0.2, 2,1.8*#PI,0)
CreateEntityBody(3, #PB_Entity_StaticBody, 1, 1, 2)
CreateSphere(#sphere, 0.3)
CreateEntity(#sphere, MeshID(#sphere), MaterialID(2), 2, 0, 0)
CreateEntityBody(#sphere,#PB_Entity_ConvexHullBody , 1, 1, 1) ;#PB_Entity_SphereBody
wireFrame = 0
Repeat
  
  Event = WindowEvent()
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/15
        MouseY = -MouseDeltaY()/15
      EndIf
      
          
      If ExamineKeyboard()
           
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_W)
          If wireFrame
             MaterialShadingMode(1, #PB_Material_Wireframe)
          wireFrame ! 1
        Else 
          MaterialShadingMode(1, #PB_Material_Solid)
          wireFrame ! 1
        EndIf
        EndIf
            
      EndIf
  
      RotateCamera(#Camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#Camera, KeyX, 0, KeyY)
      RotateEntity(3, 0,1,0, #PB_Relative)
      
      RenderWorld()
      
            
      FlipBuffers()
              
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Thank you, applePi. Good work, you are faster than me dealing with PB Ogre.
Ogre seems powerful, but i don't like so much the way PB manage commands for 3D. I found MP3D much easier and coherent.

Right, i had the texture parameters not corrected.
However your PBOgre example does repeat the pattern texture along the mesh, because you overpass the unity and go up to 4*pi.
To cover the complete width and height texture pattern I fixed the texture u,v parameters:

Code: Select all

MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,b/lt,a/l)
and in OgrePB:

Code: Select all

MeshVertexTextureCoordinate(b/lt,a/l)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Psychophanta wrote:
mpz wrote:the MP_CreateCylinder is made from a mathematic algorythmus. This code generate more vertex as needed
Aha! that's the answer i wanted to hear.
So, can we say those algorithms which build meshes (from microsoft DX3D for example) are not as good as they should?
I just realize about this stupid issue:
There is needed 2 more vertex per segment for a tube or any rounded mesh, just because the u,v texture coordinates need the first and last vertex of each ring to have the same position.
This sample helps to understand this. Run it and press right and left keys to see the difference and to understand it all:

Code: Select all

;CreateTorusArcMesh (20160204) Psychophanta
Define.f
MP_Graphics3D(640,480,0,1)
SetWindowTitle(0,"Torus arc Mesh")
maincam.i=MP_CreateCamera():pivotcam.i=MP_CreateMesh():MP_EntitySetParent(maincam,pivotcam,0):MP_PositionEntity(maincam,0,0,-5)
light.i=MP_CreateLight(2)
Structure D3DXVECTOR3
  x.f:y.f:z.f
EndStructure 
Structure Vector3D Extends D3DXVECTOR3
  m.f;<-length(modulo)
EndStructure
Macro Wrap(number,margin1,margin2)
  (margin1#+(number#)%((margin2#)-(margin1#)))
EndMacro
Macro ProductoEscalar(a,b)
  (a#\x*b#\x+a#\y*b#\y+a#\z*b#\z)
EndMacro
Macro getmodulo(v)
  (Sqr#ProductoEscalar(v#,v#))
EndMacro
Procedure Rotate_3DVector_by_Angle_adding(*fi.Vector3D,*R0.Vector3D)
  ;Esta funcion halla un vector resultado de una rotacion en el espacio de otro vector inicial.
  ;Esta funcion admite entonces, como parametros de entrada, 2 vectores:
  ; - 'Vector radio' que se desea rotar. Este vector tiene direccion no colineal con el eje de rotación.
  ; - 'Vector angulo' ('velocidad angular' * 'tiempo'), es el angulo en el que se rota el 'Vector radio' dado.
  ;     Su modulo indica el numero de radianes a rotar, su direccion indica el angulo en el espacio en el que se rota (eje de rotación)
  ;     su sentido indica el sentido de la rotacion
  ;NOTA: la funcion devuelve el vector velocidad rectilinea (3er parametro) y el nuevo vector radio (4º parametro)
  ;       pero ambos parametros se pueden omitir haciendo que la velocidad rectilinea no se devuelva y que el nuevo radio se devuelva en el 2º parametro
  Protected Rt.Vector3D,u.Vector3D,P0.Vector3D
  *fi\m=ProductoEscalar(*fi,*fi)
  If *fi\m
    u\m=ProductoEscalar(*R0,*fi)/*fi\m
    *fi\m=Sqr(*fi\m)
    ;Proyeccion de *R0 sobre *fi:
    Rt\x=u\m**fi\x
    Rt\y=u\m**fi\y
    Rt\z=u\m**fi\z
    ;Calcular P0-> (proyeccion ortogonal de *R0 sobre *fi):
    P0\x=*R0\x-Rt\x
    P0\y=*R0\y-Rt\y
    P0\z=*R0\z-Rt\z
    P0\m=getmodulo(P0)
    If P0\m=0.0:ProcedureReturn:EndIf
    ;Calcular el producto vectorial: u-> = *fi-> X P0->
    u\x=*fi\y*P0\z-*fi\z*P0\y
    u\y=*fi\z*P0\x-*fi\x*P0\z
    u\z=*fi\x*P0\y-*fi\y*P0\x
    ;ahora obtener *R0-> = (Proyeccion de *R0 sobre *fi)-> + (cos(|*fi->|)·P0-> + |P0->|/|u->|·sin(|*fi->|)·u->)->:
    u\m=getmodulo(u)
    *R0\x=Rt\x
    *R0\y=Rt\y
    *R0\z=Rt\z
    !mov edi,dword[p.p_fi]
    !fld dword[edi+12]; <- get *fi\m
    !fsincos
    !fstp dword[p.v_Rt]; <- Rt\x=Cos(*fi\m)
    !fstp dword[p.v_Rt+4]; <- Rt\y=Sin(*fi\m)
    ; o bien si no se quiere usar ensamblador sustituirlo por
    ; Rt\x=Cos(*fi\m):Rt\y=Sin(*fi\m)
    *R0\x+Rt\x*P0\x+P0\m/u\m*Rt\y*u\x
    *R0\y+Rt\x*P0\y+P0\m/u\m*Rt\y*u\y
    *R0\z+Rt\x*P0\z+P0\m/u\m*Rt\y*u\z
    *R0\m=P0\m
  EndIf
EndProcedure
Procedure.i CreateTorusArc(l.l=20,lt.l=20,r0.f=0.4,r1.f=0.4,rt.f=2,at.f=#PI,torsion.f=0.0,toffset.f=0)
  ; Crea el arco de un toroide (o un tubo recto). El toroide (o tubo recto) puede ser de grosor creciente o decreciente (cono recto).
  If l<2 Or lt<1 Or r0<0 Or r1<0:ProcedureReturn 0:EndIf
  Protected Mesh.i=MP_CreateMesh(),vert.Vector3D,ang.Vector3D,a.l,b.l,p.f,v0.l,v1.l
  ;l = numero de lados en la seccion (number of sides in cut area)
  ;lt = numero de segmentos (number of segments of torus)
  ;r0 = radio grosor inicial (radius of cut area at initial extreme)
  ;r1 = radio grosor final (radius of cut area at end extreme)
  ;rt = radio del toro (radius of torus). (NOTICE: if this parameter is 0, the function performs a tube mesh in which 'at' parameter is the heigh of the tube)
  ;at = longitud del arco del toro (arc lenght of the torus) (height of the tube if 'rt' parameter is 0)
  ;torsion = (torsion)
  ;toffset = (initial offset of the starting angle)
  If rt=0; => tube
    vert\z=-at/2*lt
    For b=0 To lt
      For a=0 To l-1
        p=r0+(r1-r0)*b/lt
        vert\x=p*Cos(2*#PI*a/l+toffset)
        vert\y=p*Sin(2*#PI*a/l+toffset)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,b/lt,a/l)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      vert\z+at
      toffset+torsion
    Next
  Else; => Torus arc
    For b=0 To lt
      For a=0 To l-1
        p=r0+(r1-r0)*b/lt
        vert\x=rt+p*Cos(2*#PI*a/l+toffset)+(r1-r0)*b/lt/2
        vert\y=p*Sin(2*#PI*a/l+toffset)
        vert\z=0
        ang\y=-at*b/lt
        Rotate_3DVector_by_Angle_adding(@ang,@vert)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,b/lt,a/l)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      toffset+torsion
    Next
  EndIf
  For b=0 To lt-1
    For a=0 To l-1
      v0=Wrap(b*l+a,b*l,b*l+l):v1=Wrap(b*l+a+l+1,b*l+l,b*l+2*l); <- uso estas variables para no recalcularlas (para ganar tiempo)
      MP_AddTriangle(Mesh,v0,Wrap(b*l+a+l,b*l+l,b*l+2*l),v1)
      MP_AddTriangle(Mesh,v0,Wrap(b*l+a+1,b*l,b*l+l),v1)
    Next
  Next
  MP_EntitySetNormals(Mesh)
  ProcedureReturn Mesh
EndProcedure
Tor.i=CreateTorusArc()
Texture.i=MP_LoadTexture(#PB_Compiler_Home+"Examples/3D/Data/Textures/MRAMOR6X6.jpg"):MP_EntitySetTexture(Tor,Texture)
While MP_KeyDown(#PB_Key_Escape)=0 And WindowEvent()<>#PB_Event_CloseWindow
  mdx=MP_MouseDeltaX()/200:mdy=MP_MouseDeltaY()/200:mdw=MP_MouseDeltaWheel()/400
  If MP_MouseButtonDown(1)
    MP_TurnEntity(pivotcam,mdy*60,mdx*60,0,0)
    If mdw
      MP_EntitySetZ(maincam,MP_EntityGetZ(maincam)+mdw); <- MP_MoveEntity(maincam,0,0,mdw)
    EndIf
    *rot=MP_EntityGetMatrix(pivotcam)
  EndIf
  If MP_KeyHit(#PB_Key_Left)
    MP_FreeEntity(Tor):Tor.i=MP_CreateCylinder(0.8,2,0.8,4,1,0)
    MP_EntitySetTexture(Tor,Texture)
  ElseIf MP_KeyHit(#PB_Key_Right)    
    MP_FreeEntity(Tor):Tor.i=CreateTorusArc(4,1,0.8,0.8,0,2,0)
    MP_EntitySetTexture(Tor,Texture)
  ElseIf MP_KeyHit(#PB_Key_W):wireframe.b!1:MP_WireFrame(wireframe.b)
  EndIf
  MP_DrawText(1,1,Str(MP_CountVertices(Tor))+", "+Str(MP_CountTriangles(Tor)))
  MP_RenderWorld()
  MP_Flip()
Wend
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
mpz
Enthusiast
Enthusiast
Posts: 494
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: MP3D Engine Alpha 32

Post by mpz »

Hi Psychophanta,

i played a little bit with your code an now i think the texure coords are working correct. Please have a look on it...

Greetings Michael

Code: Select all

;CreateTorusArcMesh (20160204) Psychophanta
Define.f
MP_Graphics3D(640,480,0,1)
SetWindowTitle(0,"Torus arc Mesh")
maincam.i=MP_CreateCamera():pivotcam.i=MP_CreateMesh():MP_EntitySetParent(maincam,pivotcam,0):MP_PositionEntity(maincam,0,0,-5)
light.i=MP_CreateLight(2)
MP_LightSetColor (light, RGB(0, 255, 0))
MP_PositionEntity(light,0,10,-2)
MP_EntityLookAt(light, 0,0,0)

Structure MyVertex
      x.f : y.f : z.f      ; vertices coordinates
      nx.f : ny.f : nz.f ; normal coordinates
      Color.l                  ; color
      u.f : v.f               ; texture coordinates
      ;u1.f : v1.f               ; texture coordinates 2
      ;u2.f : v2.f               ; texture coordinates 2
EndStructure

Structure D3DXVECTOR3
  x.f:y.f:z.f
EndStructure

Structure Vector3D Extends D3DXVECTOR3
  m.f;<-length(modulo)
EndStructure


Macro Wrap(number,margin1,margin2)
  (margin1#+(number#)%((margin2#)-(margin1#)))
EndMacro

Macro ProductoEscalar(a,b)
  (a#\x*b#\x+a#\y*b#\y+a#\z*b#\z)
EndMacro

Macro getmodulo(v)
  (Sqr#ProductoEscalar(v#,v#))
EndMacro
Procedure Rotate_3DVector_by_Angle_adding(*fi.Vector3D,*R0.Vector3D)
  ;Esta funcion halla un vector resultado de una rotacion en el espacio de otro vector inicial.
  ;Esta funcion admite entonces, como parametros de entrada, 2 vectores:
  ; - 'Vector radio' que se desea rotar. Este vector tiene direccion no colineal con el eje de rotación.
  ; - 'Vector angulo' ('velocidad angular' * 'tiempo'), es el angulo en el que se rota el 'Vector radio' dado.
  ;     Su modulo indica el numero de radianes a rotar, su direccion indica el angulo en el espacio en el que se rota (eje de rotación)
  ;     su sentido indica el sentido de la rotacion
  ;NOTA: la funcion devuelve el vector velocidad rectilinea (3er parametro) y el nuevo vector radio (4º parametro)
  ;       pero ambos parametros se pueden omitir haciendo que la velocidad rectilinea no se devuelva y que el nuevo radio se devuelva en el 2º parametro
  Protected Rt.Vector3D,u.Vector3D,P0.Vector3D
  *fi\m=ProductoEscalar(*fi,*fi)
  If *fi\m
    u\m=ProductoEscalar(*R0,*fi)/*fi\m
    *fi\m=Sqr(*fi\m)
    ;Proyeccion de *R0 sobre *fi:
    Rt\x=u\m**fi\x
    Rt\y=u\m**fi\y
    Rt\z=u\m**fi\z
    ;Calcular P0-> (proyeccion ortogonal de *R0 sobre *fi):
    P0\x=*R0\x-Rt\x
    P0\y=*R0\y-Rt\y
    P0\z=*R0\z-Rt\z
    P0\m=getmodulo(P0)
    If P0\m=0.0:ProcedureReturn:EndIf
    ;Calcular el producto vectorial: u-> = *fi-> X P0->
    u\x=*fi\y*P0\z-*fi\z*P0\y
    u\y=*fi\z*P0\x-*fi\x*P0\z
    u\z=*fi\x*P0\y-*fi\y*P0\x
    ;ahora obtener *R0-> = (Proyeccion de *R0 sobre *fi)-> + (cos(|*fi->|)·P0-> + |P0->|/|u->|·sin(|*fi->|)·u->)->:
    u\m=getmodulo(u)
    *R0\x=Rt\x
    *R0\y=Rt\y
    *R0\z=Rt\z
    !mov edi,dword[p.p_fi]
    !fld dword[edi+12]; <- get *fi\m
    !fsincos
    !fstp dword[p.v_Rt]; <- Rt\x=Cos(*fi\m)
    !fstp dword[p.v_Rt+4]; <- Rt\y=Sin(*fi\m)
    ; o bien si no se quiere usar ensamblador sustituirlo por
    ; Rt\x=Cos(*fi\m):Rt\y=Sin(*fi\m)
    *R0\x+Rt\x*P0\x+P0\m/u\m*Rt\y*u\x
    *R0\y+Rt\x*P0\y+P0\m/u\m*Rt\y*u\y
    *R0\z+Rt\x*P0\z+P0\m/u\m*Rt\y*u\z
    *R0\m=P0\m
  EndIf
EndProcedure

Procedure.i CreateTorusArc(l.l=20,lt.l=20,r0.f=0.4,r1.f=0.4,rt.f=2,at.f=#PI,torsion.f=0.0,toffset.f=0)
  ; Crea el arco de un toroide (o un tubo recto). El toroide (o tubo recto) puede ser de grosor creciente o decreciente (cono recto).
  If l<2 Or lt<1 Or r0<0 Or r1<0:ProcedureReturn 0:EndIf
  Protected Mesh.i=MP_CreateMesh(),vert.Vector3D,ang.Vector3D,a.l,b.l,p.f,v0.l,v1.l,offset.i 
  ;l = numero de lados en la seccion (number of sides in cut area)
  ;lt = numero de segmentos (number of segments of torus)
  ;r0 = radio grosor inicial (radius of cut area at initial extreme)
  ;r1 = radio grosor final (radius of cut area at end extreme)
  ;rt = radio del toro (radius of torus). (NOTICE: if this parameter is 0, the function performs a tube mesh in which 'at' parameter is the heigh of the tube)
  ;at = longitud del arco del toro (arc lenght of the torus) (height of the tube if 'rt' parameter is 0)
  ;torsion = (torsion)
  ;toffset = (initial offset of the starting angle)
  If rt=0; => tube
    vert\z=-at/2*lt
    For b=0 To lt
      For a=0 To l;-1
        p=r0+(r1-r0)*b/lt
        vert\x=p*Cos(2*#PI*a/l+toffset)
        vert\y=p*Sin(2*#PI*a/l+toffset)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,a/l,b/lt)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      vert\z+at
      toffset+torsion
    Next
  Else; => Torus arc
    For b=0 To lt
      For a=0 To l;-1
        p=r0+(r1-r0)*b/lt
        vert\x=rt+p*Cos(2*#PI*a/l+toffset)+(r1-r0)*b/lt/2
        vert\y=p*Sin(2*#PI*a/l+toffset)
        vert\z=0
        ang\y=-at*b/lt
        Rotate_3DVector_by_Angle_adding(@ang,@vert)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,a/l,b/lt)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      toffset+torsion
    Next
  EndIf
  
  offset.i = 0
  For b=0 To lt-1
    For a=0 To l
       If  a <> l
         MP_AddTriangle(Mesh,offset + l + 2, offset + l + 1 ,offset + 1)    	
         MP_AddTriangle(Mesh,offset + l + 1,offset ,offset + 1)
       EndIf
       offset + 1
    Next
  Next
  
  MP_EntitySetNormals(Mesh)
  ProcedureReturn Mesh
  
EndProcedure

Tor.i=CreateTorusArc()

If CreateImage(0, 255, 255)

    Font = LoadFont(#PB_Any, "Arial"  , 138) 
    StartDrawing(ImageOutput(0))

    Box(0, 0, 128, 128,RGB(255,0,0))
    Box(128, 0, 128, 128,RGB(0,255,0))
    Box(0, 128, 128, 128,RGB(0,0,255))
    Box(128, 128, 128, 128,RGB(255,255,0))

    DrawingFont(FontID(Font))
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawText(73,35,"5",RGB(0,0,0))
  
    StopDrawing() ; This is absolutely needed when the drawing operations are finished !!! Never forget it !
    
EndIf

Texture.i = MP_ImageToTexture(0)
;Texture.i=MP_LoadTexture(#PB_Compiler_Home+"Examples/3D/Data/Textures/MRAMOR6X6.jpg")
MP_EntitySetTexture(Tor,Texture)

sphere.i = MP_CreateSphere(16): MP_ScaleMesh(sphere, 0.1, 0.1, 0.1)
vertexcount.i = MP_CountVertices(Tor)
vertexlenght.i = MP_GetMeshInfo (Tor, 64)
Dim  MyVert.MyVertex (vertexcount)
MP_GetMeshData(Tor, #PB_Mesh_Vertex,@MyVert(),vertexlenght * vertexcount)


While MP_KeyDown(#PB_Key_Escape)=0 And WindowEvent()<>#PB_Event_CloseWindow
  mdx=MP_MouseDeltaX()/200:mdy=MP_MouseDeltaY()/200:mdw=MP_MouseDeltaWheel()/400
  If MP_MouseButtonDown(1)
    MP_TurnEntity(pivotcam,mdy*60,mdx*60,0,0)
    If mdw
      MP_EntitySetZ(maincam,MP_EntityGetZ(maincam)+mdw); <- MP_MoveEntity(maincam,0,0,mdw)
    EndIf
    *rot=MP_EntityGetMatrix(pivotcam)
  EndIf
  If MP_KeyHit(#PB_Key_Left)
    MP_FreeEntity(Tor):Tor.i=MP_CreateCylinder(0.8,4,0.8,4,1,0)
    MP_EntitySetTexture(Tor,Texture)
  ElseIf MP_KeyHit(#PB_Key_Right)   
    MP_FreeEntity(Tor):Tor.i=CreateTorusArc(4,1,1,1,0,4,0)
    vertexcount.i = MP_CountVertices(Tor)
    vertexlenght.i = MP_GetMeshInfo (Tor, 64)
    ReDim  MyVert.MyVertex (vertexcount-1)
    MP_GetMeshData(Tor, #PB_Mesh_Vertex,@MyVert(),vertexlenght * vertexcount)
    MP_EntitySetTexture(Tor,Texture)
  ElseIf MP_KeyHit(#PB_Key_W):wireframe.b!1:MP_WireFrame(wireframe.b)
  EndIf
  
  If MP_KeyHit(#PB_Key_Z)
    n.i+1
    If n = vertexcount 
      n=-1
      MP_PositionEntity(sphere,0,0,0)
  Else  
    MP_PositionEntity(sphere,Myvert(n)\x,Myvert(n)\y,Myvert(n)\z)
  EndIf  
       
  EndIf
  
  MP_DrawText(1,1,"Index = " +Str(n)+"  ......"+Str(MP_CountVertices(Tor))+",   "+Str(MP_CountTriangles(Tor))+",   "+MP_CountSurfaces(Tor))
  MP_RenderWorld()
  MP_Flip()
Wend
Working on - MP3D Library - PB 5.73 version ready for download
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Thank you Michael.

I started to fix it, but i had no time, i am preparing for travel tomorrow.
You have contributed again to save me for doing this task, good work!
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Hallo all!

I have an issue I think applePi can help and learn on it.
I want to do a car and move it just like a real car, this is, performing a torque in its wheels.
So i have this:

Code: Select all

Define .f
ExamineDesktops()
Global bitplanes.a=DesktopDepth(0),RX.u=DesktopWidth(0),RY.u=DesktopHeight(0)
If RX<1280 Or RY<720:RX*2/3:RY*2/3:Else:RX=1280:RY=720:EndIf:MP_Graphics3DWindow(0,0,RX,RY,"",#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SystemMenu)
; MP_Graphics3D(RX,RY,bitplanes,0)
SetWindowTitle(0,"car")
MP_PhysicInit()
;/ Luz
light.i=MP_CreateLight(1)
MP_LightSetColor(light,$aaaaaa)
MP_PositionEntity(light,4,4,0)
MP_CreateSkyBox("..\media\skyboxsun5deg","jpg",100)
;\
;/ cam
cam.i=MP_CreateCamera():pivotcam.i=MP_CreateMesh():MP_EntitySetParent(cam,pivotcam,0):MP_PositionEntity(cam,0,-2,-15)
MP_TurnEntity(pivotcam,4,36,0,0)
;\
;/ fondo
fondo.i=MP_CreateRectangle(256,0,256)
MP_PositionEntity(fondo,0,-4.5,0)
fondotexture.i=MP_LoadTexture("..\media\wood-floor2.jpg",0,0)
MP_EntitySetTexture(fondo,fondotexture,0,0)
MP_EntityPhysicBody(fondo,1,5)
;\
Esqueleto.i=MP_CreateCube():MP_ResizeMesh(Esqueleto,4,1,12)
MP_MaterialDiffuseColor(Esqueleto,$aa,$BE,$DA,$DB)
MP_EntityPhysicBody(esqueleto,4,1)
ruedaDD.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaDD,0,90,0):MP_PositionEntity(ruedaDD,2,-2,6)
MP_EntityPhysicBody(ruedaDD,2,1):MP_ConstraintCreateHinge(ruedaDD,1,0,0,0,0,0,Esqueleto)

ruedaDI.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaDI,0,90,0):MP_PositionEntity(ruedaDI,-2,-2,6)
MP_EntityPhysicBody(ruedaDI,2,1):MP_ConstraintCreateHinge(ruedaDI,1,0,0,0,0,0,Esqueleto)

ruedaTD.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaTD,0,90,0):MP_PositionEntity(ruedaTD,2,-2,-6)
MP_EntityPhysicBody(ruedaTD,2,1):MP_ConstraintCreateHinge(ruedaTD,1,0,0,0,0,0,Esqueleto)

ruedaTI.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaTI,0,90,0):MP_PositionEntity(ruedaTI,-2,-2,-6)
MP_EntityPhysicBody(ruedaTI,2,1):MP_ConstraintCreateHinge(ruedaTI,1,0,0,0,0,0,Esqueleto)

dist.f=10
MP_MouseInWindow()
MP_UseCursor(0)
While MP_KeyDown(#PB_Key_Escape)=0 And WindowEvent()<>#PB_Event_CloseWindow
  mdx=MP_MouseDeltaX()/200:mdy=MP_MouseDeltaY()/200:mdw=MP_MouseDeltaWheel()/400
  If mdw
    MP_EntitySetZ(cam,MP_EntityGetZ(cam)+mdw); <- MP_MoveEntity(cam,0,0,mdw)
  ElseIf MP_MouseButtonDown(1)
    MP_MoveEntity(pivotcam,mdx,-mdy,0)
  ElseIf mdy Or mdx
    MP_TurnEntity(pivotcam,mdy*60,mdx*60,0,0)
  ElseIf MP_KeyDown(#PB_Key_Right)
    MP_EntityAddImpulse(ruedaDD,0,0,1,0,dist,0):MP_EntityAddImpulse(ruedaDD,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaDI,0,0,1,0,dist,0):MP_EntityAddImpulse(ruedaDI,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTD,0,0,1,0,dist,0):MP_EntityAddImpulse(ruedaTD,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTI,0,0,1,0,dist,0):MP_EntityAddImpulse(ruedaTI,0,0,-1,0,-dist,0)
  ElseIf MP_KeyDown(#PB_Key_Left)
    MP_EntityAddImpulse(ruedaDD,0,0,-1,0,dist,0):MP_EntityAddImpulse(ruedaDD,0,0,1,0,-dist,0)
    MP_EntityAddImpulse(ruedaDI,0,0,-1,0,dist,0):MP_EntityAddImpulse(ruedaDI,0,0,1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTD,0,0,-1,0,dist,0):MP_EntityAddImpulse(ruedaTD,0,0,1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTI,0,0,-1,0,dist,0):MP_EntityAddImpulse(ruedaTI,0,0,1,0,-dist,0)
  EndIf
  MP_PhysicUpdate()
  MP_RenderWorld()
  MP_Flip():Delay(8)
Wend
MP_PhysicEnd()
As you can see, when there is made a torque in each wheel, the car does not move on the floor. Instead it is inclinated. Why it happens?, How to solve it?

There is also another issue:
I also wanto to make a device (a Cylinder) between the wheel and the body in a way like a "shock absorber", but it should be extended or retracted in a customized way by the program.
I think it must be done using MP_ConstraintCreateSlider() function, but i don't know how. If someone here can point me it would be great and i would be very grateful!

Salud! :)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MP3D Engine Alpha 32

Post by applePi »

Hi Psychophanta
i will think about your car later, but now :you have used rectangle physics(2) for the wheels , i am not sure to use a sphere physics(3) or free object (4)
also (but not sure) we may use a friction for the ground and for the wheels
test this, i will more later
also look your code here and the other wheels:
MP_EntityAddImpulse(ruedaDD,0,0,1,0,dist,0):MP_EntityAddImpulse(ruedaDD,0,0,-1,0,-dist,0)
you have contradictory forces for the same wheel

Code: Select all

Define .f
ExamineDesktops()
Global bitplanes.a=DesktopDepth(0),RX.u=DesktopWidth(0),RY.u=DesktopHeight(0)
If RX<1280 Or RY<720:RX*2/3:RY*2/3:Else:RX=1280:RY=720:EndIf:MP_Graphics3DWindow(0,0,RX,RY,"",#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SystemMenu)
; MP_Graphics3D(RX,RY,bitplanes,0)
SetWindowTitle(0,"car")
MP_PhysicInit()
MP_EntityPhysicBody(bitplanes,1,1)
NewMaterial = MP_CreatePhysicMaterial() 
MP_SetPhysicMaterialProperties(NewMaterial,0,0.4,1)
MP_SetPhysicMaterialtoMesh (bitplanes, NewMaterial)
;/ Luz
light.i=MP_CreateLight(1)
MP_LightSetColor(light,$aaaaaa)
MP_PositionEntity(light,4,4,0)
MP_CreateSkyBox("..\media\skyboxsun5deg","jpg",100)
;\
;/ cam
cam.i=MP_CreateCamera():pivotcam.i=MP_CreateMesh():MP_EntitySetParent(cam,pivotcam,0):MP_PositionEntity(cam,0,-2,-15)
MP_TurnEntity(pivotcam,4,36,0,0)
;\
;/ fondo
fondo.i=MP_CreateRectangle(256,0,256)
MP_PositionEntity(fondo,0,-4.5,0)
fondotexture.i=MP_LoadTexture("..\media\wood-floor2.jpg",0,0)
MP_EntitySetTexture(fondo,fondotexture,0,0)
MP_EntityPhysicBody(fondo,1,5)
tex.i = MP_LoadTexture(#PB_Compiler_Home + "examples/3d/Data/Textures/MRAMOR6X6.jpg",0,0) 
;\
Esqueleto.i=MP_CreateCube():MP_ResizeMesh(Esqueleto,4,1,12)
MP_MaterialDiffuseColor(Esqueleto,$aa,$BE,$DA,$DB)
MP_EntityPhysicBody(esqueleto,4,1)
ruedaDD.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaDD,0,90,0)
MP_PositionEntity(ruedaDD,2,-2,6)
MP_EntitySetTexture(ruedaDD,tex,0,0)
MP_EntityPhysicBody(ruedaDD,4,1):MP_ConstraintCreateHinge(ruedaDD,1,0,0,0,0,0,Esqueleto)
MP_SetPhysicMaterialtoMesh (ruedaDD, NewMaterial)

ruedaDI.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaDI,0,90,0)
MP_PositionEntity(ruedaDI,-2,-2,6)
MP_EntitySetTexture(ruedaDI,tex,0,0)
MP_EntityPhysicBody(ruedaDI,4,1):MP_ConstraintCreateHinge(ruedaDI,1,0,0,0,0,0,Esqueleto)
MP_SetPhysicMaterialtoMesh (ruedaDI, NewMaterial)

ruedaTD.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaTD,0,90,0)
MP_PositionEntity(ruedaTD,2,-2,-6)
MP_EntitySetTexture(ruedaTD,tex,0,0)
MP_EntityPhysicBody(ruedaTD,4,1):MP_ConstraintCreateHinge(ruedaTD,1,0,0,0,0,0,Esqueleto)
MP_SetPhysicMaterialtoMesh (ruedaTD, NewMaterial)

ruedaTI.i=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(ruedaTI,0,90,0)
MP_PositionEntity(ruedaTI,-2,-2,-6)
MP_EntitySetTexture(ruedaTI,tex,0,0)
MP_EntityPhysicBody(ruedaTI,4,1):MP_ConstraintCreateHinge(ruedaTI,1,0,0,0,0,0,Esqueleto)
MP_SetPhysicMaterialtoMesh (ruedaTI, NewMaterial)

dist.f=10
MP_MouseInWindow()
MP_UseCursor(0)
While MP_KeyDown(#PB_Key_Escape)=0 And WindowEvent()<>#PB_Event_CloseWindow
  mdx=MP_MouseDeltaX()/200:mdy=MP_MouseDeltaY()/200:mdw=MP_MouseDeltaWheel()/400
  If mdw
    MP_EntitySetZ(cam,MP_EntityGetZ(cam)+mdw); <- MP_MoveEntity(cam,0,0,mdw)
  ElseIf MP_MouseButtonDown(1)
    MP_MoveEntity(pivotcam,mdx,-mdy,0)
  ElseIf mdy Or mdx
    MP_TurnEntity(pivotcam,mdy*60,mdx*60,0,0)
  ElseIf MP_KeyDown(#PB_Key_Right)
    MP_EntityAddImpulse(ruedaDD,0,0,1,0,dist,0);:MP_EntityAddImpulse(ruedaDD,0,0,1,0,dist,0)
    MP_EntityAddImpulse(ruedaDI,0,0,1,0,dist,0);:MP_EntityAddImpulse(ruedaDI,0,0,1,0,dist,0)
    MP_EntityAddImpulse(ruedaTD,0,0,1,0,dist,0);:MP_EntityAddImpulse(ruedaTD,0,0,1,0,dist,0)
    MP_EntityAddImpulse(ruedaTI,0,0,1,0,dist,0);:MP_EntityAddImpulse(ruedaTI,0,0,1,0,dist,0)
  ElseIf MP_KeyDown(#PB_Key_Left)
    MP_EntityAddImpulse(ruedaDD,0,0,-1,0,dist,0);:MP_EntityAddImpulse(ruedaDD,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaDI,0,0,-1,0,dist,0);:MP_EntityAddImpulse(ruedaDI,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTD,0,0,-1,0,dist,0);:MP_EntityAddImpulse(ruedaTD,0,0,-1,0,-dist,0)
    MP_EntityAddImpulse(ruedaTI,0,0,-1,0,dist,0);:MP_EntityAddImpulse(ruedaTI,0,0,-1,0,-dist,0)
  EndIf
  MP_PhysicUpdate()
  MP_RenderWorld()
  MP_Flip():Delay(8)
Wend
MP_PhysicEnd()
mpz
Enthusiast
Enthusiast
Posts: 494
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: MP3D Engine Alpha 32

Post by mpz »

Hi,

car driving ...cool. Perhaps we can look on the newton dynamics car demo and if some newton command is missing i will integrate them in the engine...

Greetings Michael
Working on - MP3D Library - PB 5.73 version ready for download
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MP3D Engine Alpha 32

Post by applePi »

Psychophanta: I also wanto to make a device (a Cylinder) between the wheel and the body in a way like a "shock absorber", but it should be extended or retracted in a customized way by the program.
yes it is possible to make what resembles a shock absorber by making 2 joints very near to each other so it is like a spring, look my post here
http://purebasic.fr/english/viewtopic.p ... 85#p468095
when we drop a weight over a rectangle it will swing back and forth as it is a spring
i have experimented before a year in a car with PB ogre-bullet engine every wheel are attached to a rectangle (shock absorber) by hinge Joint , the rectangle (shock absorber) attached to the car body with other 2 hinge joints separated a little, look at this automatic car and you will see it really springy, press 'W' to see its internals in wireFrame. i haven't published the code since i was not happy with the general results and the designing of joints is too complex, but it show the case, and if you run the code use pb5.40 and not 5.42.
also note this line:
SetEntityAttribute(#spring, #PB_Entity_DisableContactResponse, 1)
#PB_Entity_DisableContactResponse disable the contact between the spring and any other body but its joints still active.
Image

works only with PB 5.40

Code: Select all

Enumeration
   
   #camera
   #Plane
   #cyl
   #cyl2
   #cyl3
   #spring
   #spring2
   #cone
   #cone2
EndEnumeration


Define.f KeyX, KeyY, MouseX, MouseY

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "change direction with 1, 2, ... press 'W': wire/solid frame .....use mouse+Arrows to rotate/move Camera", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Scripts",#PB_3DArchive_FileSystem)
    
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)
    
              
    CreateCamera(#camera, 0, 0, 100, 100)
    MoveCamera(#camera, 20, 10, -20, #PB_Absolute)
    CameraBackColor(#camera, RGB(255,200,200))
    CameraLookAt(#camera,0,3,0)
        
    CreateLight(0, RGB(255,255,255), 0, 20, 30)
    AmbientColor(RGB(200, 200, 200))
    
    CreateMaterial(3, LoadTexture(3, "ValetCoeur.jpg"))
    DisableMaterialLighting(3, #False)
    SetMaterialColor(3, #PB_Material_AmbientColor, RGB(250, 255, 0))
    SetMaterialColor(3, #PB_Material_SpecularColor, RGB(255, 255, 0))
    
    CreateMaterial(1, LoadTexture(1, "MRAMOR6X6.jpg"))
    MaterialCullingMode(1, #PB_Material_NoCulling)
    CreateMaterial(0, LoadTexture(0, "Dirt.jpg"))
        
    CreateMaterial(4, LoadTexture(4, "ground_diffuse.png"))
    MaterialCullingMode(4, #PB_Material_NoCulling)
        
    CreatePlane(#Plane, 1000, 1000, 5, 5, 2, 2) ; the ground
    CreateEntity(#Plane, MeshID(#Plane), MaterialID(4), 0,-4,0)
    CreateEntityBody(#Plane, #PB_Entity_StaticBody, 1, 0.1, 2)

    
    CreateCube(0,1) 
    CreateEntity(0, MeshID(0), MaterialID(1), 0, 0, 0) ; car body
    ScaleEntity(0, 3,3,6)
    CreateCylinder(#cyl, 1, 5)
    CreateEntity(#cyl, MeshID(#cyl), MaterialID(3), -5,0, 0) 
    RotateEntity(#cyl,0,0,90)
    ScaleEntity(#cyl, 3,0.5,3)
    CreateEntity(#cyl2, MeshID(#cyl), MaterialID(1), 5,0, 0) 
    RotateEntity(#cyl2,0,0,90)
    ScaleEntity(#cyl2, 3,0.5,3)
    CreateEntity(#cyl3, MeshID(#cyl), MaterialID(3), 0,0, 7)
    ;RotateEntity(#cyl3,0,0,90) ; uncomment this line to have a funny effect
    ScaleEntity(#cyl3, 3,0.1,3)
    
    CreateEntity(#spring, MeshID(0), MaterialID(3), 6,0, 0) ; the spring
    ScaleEntity(#spring, 6,0.5,2)
    CreateEntity(#spring2, MeshID(0), MaterialID(3), -6,0, 0) ; the spring
    ScaleEntity(#spring2, 6,0.5,2)
        
    CreateEntityBody(#spring, #PB_Entity_BoxBody, 1, 0.1, 1)  
    CreateEntityBody(#spring2, #PB_Entity_BoxBody, 1, 0.1, 1) 
    
    CreateEntityBody(0, #PB_Entity_BoxBody, 90, 0.1,10)
    CreateEntityBody(#cyl, #PB_Entity_CylinderBody, 100, 0.1, 4) 
    CreateEntityBody(#cyl2,#PB_Entity_CylinderBody, 100, 0.1, 4) 
    CreateEntityBody(#cyl3,#PB_Entity_CylinderBody, 10, 1.1, 4)

     ;---------------------------------------------------
    
     HingeJoint(1, EntityID(0),
               0, 0, 0,
               0, 0, 1, 
               EntityID(#spring),
               -3, 0, 0, 
               0, 0, 1)
     HingeJoint(2, EntityID(0),
                1.5, 0, 0,
                0, 0, 1, 
                EntityID(#spring),
                -1.5, 0, 0, 
                0, 0, 1) 
     
     
     HingeJoint(3, EntityID(#spring),
                1, 0, 0,
                -1, 0, 0, 
                EntityID(#cyl2),
                0, 0.5, 0, 
                0, 1, 0)
     
     ;;---------------------------------------------------
     HingeJoint(4, EntityID(0),
                0, 0, 0,
                0, 0, 1, 
                EntityID(#spring2),
                3, 0, 0, 
                0, 0, 0)
     
     HingeJoint(5, EntityID(0),
                -2, 0, 0,
                0, 0, 1, 
                EntityID(#spring2),
                2, 0, 0, 
                0, 0, -1) 
     
     
     HingeJoint(6, EntityID(#spring2),
                -1, 0, 0,
                1, 0, 0, 
                EntityID(#cyl),
                0, 0.5, 0, 
                0, -1, 0)
     
     
     ;;---------------------------------------------------
     HingeJoint(7, EntityID(0), ; front wheel
                0, 0, 7,
                1, 0, 0,
                EntityID(#cyl3),
                0, 0, 0,
                0, 1, 0)

    CreateCone(#cone, 4,5)
    CreateEntity(#cone, MeshID(#cone), MaterialID(4), 0,2,0)
    AttachEntityObject(0, "",EntityID(#cone))
    
    CreateCone(#cone2, 4,5, 4, 16)
    CreateEntity(#cone2, MeshID(#cone2), MaterialID(0), 5,0,10)
    TransformMesh(#cone2, 0,0,0, 1,1,0.5,0,0,0)
    RotateEntity(#cone2, -90,0,0)
    MoveEntity(#cone2, 0,-4,0)
    CreateEntityBody(#cone2, #PB_Entity_StaticBody)
    
;TransformMesh(#Mesh, x, y, z, ScaleX, ScaleY, ScaleZ, RotateX, RotateY, RotateZ [, SubMesh])
                  
    
    SetEntityAttribute(#spring, #PB_Entity_DisableContactResponse, 1)
    SetEntityAttribute(#spring2, #PB_Entity_DisableContactResponse, 1)
    
    autoGear = 1  : wireFrame = 1             
    Repeat
      Repeat
      Until WindowEvent() = 0
        
      If ExamineMouse()
        MouseX = -MouseDeltaX()/20 
        MouseY = -MouseDeltaY()/20
      EndIf
      
          
      If ExamineKeyboard()
         
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -1
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = 1
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -1
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = 1
        Else
          KeyY = 0
        EndIf
        
        If KeyboardReleased(#PB_Key_W)
          If wireFrame
            MaterialShadingMode(1, #PB_Material_Wireframe)
            wireFrame ! 1
          Else
            MaterialShadingMode(1, #PB_Material_Solid)
            wireFrame ! 1
          EndIf
          
        EndIf
        
        
        If KeyboardReleased(#PB_Key_1)
          autoGear = 1
        ElseIf KeyboardReleased(#PB_Key_2)
          autoGear = 2
        EndIf
        
        Select autoGear
          Case 1
            ApplyEntityTorque(#cyl,  1000, 0, 0)
            ApplyEntityTorque(#cyl2, 1000, 0, 0)
          Case 2
            ApplyEntityTorque(#cyl, -3000, 0, 0)
            ApplyEntityTorque(#cyl2, -3000, 0, 0)
        EndSelect
                   
          
       EndIf
       
          
      RotateCamera(#camera, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera(#camera, KeyX, 0, KeyY)
      
      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Thank you applePi,

yes, it was useful your demo using 2 constraints very near one from the other.
However the thing wished is not a normal shock absorber, but a way or strategy to lift, or to shift an entity respect form other one.
At the moment i am dealing with: MP_ConstraintCreateSlider()

Yes @mpz, there would be useful some stuff for wheels, shock absorbers, helix, etc.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Mythros
Enthusiast
Enthusiast
Posts: 306
Joined: Mon Aug 19, 2013 3:28 pm

Re: MP3D Engine Alpha 32

Post by Mythros »

Hi, mpz! Long time, no see! I was just wondering if there was a way to get a count for the total amount of frames in ANY loaded, animated B3D file, X file, 3DS file, & OBJ file?

If not, could you add a small patch to do that? I could REALLY use this for my Animation system!

Thanks a ton!

Mythros
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

applePi wrote: SetEntityAttribute(#spring, #PB_Entity_DisableContactResponse, 1)
#PB_Entity_DisableContactResponse disable the contact between the spring and any other body but its joints still active.
Interesting. Is it not available in MP3D?

MP_EntityAddImpulse()

Syntax

MP_EntityAddImpulse(Entity, pointDeltaVelocX.f, pointDeltaVelocY.f, pointDeltaVelocZ.f [, pointPositX.f, pointPositY.f, pointPositZ.f])
Beschreibung


The function MP_EntitySetVelocity set a velocita to a entity. An entity is for this command, a mesh or Partikelemmitter.

Die Funktion MP_EntitySetVelocity setzt eine Geschwindigkeit in Richtung x,y,z. Ein Entity ist für diesen Befehl ein Mesh oder Partikelemmitter.

Parameter
Entity = Entity identity
Identität eines Entitys
pointDeltaVelocX.f = x-direction as a float
x-Richtung als Float
pointDeltaVelocy.f = y-direction as a float
y-Richtung als Float
pointDeltaVelocX.f = z-direction as a float
z-Richtung als Float
pointPositX.f = x-Position as float
x-Position als Float
pointPositX.f = y-Position as float
y-Position als Float
pointPositX.f = z-Position as float
z-Position als Float

return value
#True on success or #False on failure
#True oder im Fehlerfall #False
I am having strange issues with this.
Are Point position coordenates global or referred to the entity position?
Are also delta velocity coordenates a global referred vector or are them referred to the current rotation of the entity?

Thanks!
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
mpz
Enthusiast
Enthusiast
Posts: 494
Joined: Sat Oct 11, 2008 9:07 pm
Location: Germany, Berlin > member German forum

Re: MP3D Engine Alpha 32

Post by mpz »

Hi to all,

@Mythros, for now the engine only load directx anim mesh. I will have a look on it to count the frames. Perhaps i find a solution

@Psychopanta

Code: Select all

SetEntityAttribute(#spring, #PB_Entity_DisableContactResponse, 1)
#PB_Entity_DisableContactResponse disable the contact between the spring and any other body but its joints still active. 
is not integrated, but i will have a look on it too

MP_EntityAddImpulse(Entity, pointDeltaVelocX.f, pointDeltaVelocY.f, pointDeltaVelocZ.f [, pointPositX.f, pointPositY.f, pointPositZ.f])
is the same command like "NewtonBodyAddImpulse"
http://newtondynamics.com/wiki/index.ph ... AddImpulse
pointDeltaVeloc - containing the desired change in velocity to point pointPosit.
pointPosit - center of the impulse in global space.

Greetings Michael
Working on - MP3D Library - PB 5.73 version ready for download
User avatar
Psychophanta
Addict
Addict
Posts: 4969
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: MP3D Engine Alpha 32

Post by Psychophanta »

Thanks Michael.

I have an idea which perhaps you are agree:
Could you make MP3D to accept inlined direct newtondynamics functions?

EDIT:
I wrote this, which works more or less, but I dont understand why the body of the car swings because the limit of the Slider has only a length of 0.01, any suggestion?
NOTICE: use cursor keys to move car, use 'W' key to down the body and 'O' key to lift the body. Use the background sky and the floor texture of your preference.

Code: Select all

Define .f
ExamineDesktops()
Global bitplanes.a=DesktopDepth(0),RX.u=DesktopWidth(0),RY.u=DesktopHeight(0)
If RX<1280 Or RY<720:RX*2/3:RY*2/3:Else:RX=1280:RY=720:EndIf:MP_Graphics3DWindow(0,0,RX,RY,"",#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SystemMenu)
; MP_Graphics3D(RX,RY,bitplanes,0)
SetWindowTitle(0,"car")
Global ShowCursor.b=0,CursorX=RX/2,CursorY=RY/2,showgui.b=1,mdx,mdy
Global font.i=MP_LoadFont("Tahoma",20,1,0)
Structure D3DMATRIX
  _11.f : _12.f : _13.f : _14.f
  _21.f : _22.f : _23.f : _24.f
  _31.f : _32.f : _33.f : _34.f
  _41.f : _42.f : _43.f : _44.f
EndStructure
Structure D3DXVECTOR3
  x.f
  y.f
  z.f
EndStructure
Structure Vector3D Extends D3DXVECTOR3
  m.f;<-length(modulo)
EndStructure
Macro ProductoEscalar(a,b)
  (a#\x*b#\x+a#\y*b#\y+a#\z*b#\z)
EndMacro
Macro getmodulo(v)
  (Sqr#ProductoEscalar(v#,v#))
EndMacro
Procedure Rotate_3DVector_by_Angle_adding(*fi.Vector3D,*R0.Vector3D)
  ;Esta funcion halla un vector resultado de una rotacion en el espacio de otro vector inicial.
  ;Esta funcion admite entonces, como parametros de entrada, 2 vectores:
  ; - 'Vector radio' que se desea rotar. Este vector tiene direccion no colineal con el eje de rotación.
  ; - 'Vector angulo' ('velocidad angular' * 'tiempo'), es el angulo en el que se rota el 'Vector radio' dado.
  ;     Su modulo indica el numero de radianes a rotar, su direccion indica el angulo en el espacio en el que se rota (eje de rotación)
  ;     su sentido indica el sentido de la rotacion
  ;NOTA: la funcion devuelve el vector velocidad rectilinea (3er parametro) y el nuevo vector radio (4º parametro)
  ;       pero ambos parametros se pueden omitir haciendo que la velocidad rectilinea no se devuelva y que el nuevo radio se devuelva en el 2º parametro
  Protected Rt.Vector3D,u.Vector3D,P0.Vector3D
  *fi\m=ProductoEscalar(*fi,*fi)
  If *fi\m
    u\m=ProductoEscalar(*R0,*fi)/*fi\m
    *fi\m=Sqr(*fi\m)
    ;Proyeccion de *R0 sobre *fi:
    Rt\x=u\m**fi\x
    Rt\y=u\m**fi\y
    Rt\z=u\m**fi\z
    ;Calcular P0-> (proyeccion ortogonal de *R0 sobre *fi):
    P0\x=*R0\x-Rt\x
    P0\y=*R0\y-Rt\y
    P0\z=*R0\z-Rt\z
    P0\m=getmodulo(P0)
    If P0\m=0.0:ProcedureReturn:EndIf
    ;Calcular el producto vectorial: u-> = *fi-> X P0->
    u\x=*fi\y*P0\z-*fi\z*P0\y
    u\y=*fi\z*P0\x-*fi\x*P0\z
    u\z=*fi\x*P0\y-*fi\y*P0\x
    ;ahora obtener *R0-> = (Proyeccion de *R0 sobre *fi)-> + (cos(|*fi->|)·P0-> + |P0->|/|u->|·sin(|*fi->|)·u->)->:
    u\m=getmodulo(u)
    *R0\x=Rt\x
    *R0\y=Rt\y
    *R0\z=Rt\z
    !mov edi,dword[p.p_fi]
    !fld dword[edi+12]; <- get *fi\m
    !fsincos
    !fstp dword[p.v_Rt]; <- Rt\x=Cos(*fi\m)
    !fstp dword[p.v_Rt+4]; <- Rt\y=Sin(*fi\m)
    ; o bien si no se quiere usar ensamblador sustituirlo por
    ; Rt\x=Cos(*fi\m):Rt\y=Sin(*fi\m)
    *R0\x+Rt\x*P0\x+P0\m/u\m*Rt\y*u\x
    *R0\y+Rt\x*P0\y+P0\m/u\m*Rt\y*u\y
    *R0\z+Rt\x*P0\z+P0\m/u\m*Rt\y*u\z
    *R0\m=P0\m
  EndIf
EndProcedure
Procedure.i CreateTorusArc(l.l=20,lt.l=20,r0.f=0.4,r1.f=0.4,rt.f=2,at.f=#PI,torsion.f=0.0,toffset.f=0)
  ; Crea el arco de un toroide (o un tubo recto). El toroide (o tubo recto) puede ser de grosor creciente o decreciente (cono recto).
  If l<2 Or lt<1 Or r0<0 Or r1<0:ProcedureReturn 0:EndIf
  Protected Mesh.i=MP_CreateMesh(),vert.Vector3D,ang.Vector3D,a.l,b.l,p.f,offset.l
  ;l = numero de lados en la seccion (number of sides in cut area)
  ;lt = numero de segmentos (number of segments of torus)
  ;r0 = radio grosor inicial (radius of cut area at initial extreme)
  ;r1 = radio grosor final (radius of cut area at end extreme)
  ;rt = radio del toro (radius of torus). (NOTICE: if this parameter is 0, the function performs a tube mesh in which 'at' parameter is the heigh of the tube)
  ;at = longitud del arco del toro (arc lenght of the torus) (height of the tube if 'rt' parameter is 0)
  ;torsion = (torsion)
  ;toffset = (initial offset of the starting angle)
  If rt=0; => tube
    vert\z=-at/2*lt
    For b=0 To lt
      For a=0 To l;-1
        p=r0+(r1-r0)*b/lt
        vert\x=p*Cos(2*#PI*a/l+toffset)
        vert\y=p*Sin(2*#PI*a/l+toffset)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,a/l,b/lt)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      vert\z+at
      toffset+torsion
    Next
  Else; => Torus arc
    For b=0 To lt
      For a=0 To l;-1
        p=r0+(r1-r0)*b/lt
        vert\x=rt+p*Cos(2*#PI*a/l+toffset)+(r1-r0)*b/lt/2
        vert\y=p*Sin(2*#PI*a/l+toffset)
        vert\z=0
        ang\y=-at*b/lt
        Rotate_3DVector_by_Angle_adding(@ang,@vert)
        MP_AddVertex(Mesh,vert\x,vert\y,vert\z,0,a/l,b/lt)
        ;sp.i=MP_CreateSphere(8):MP_ResizeMesh(sp,0.1,0.1,0.1):MP_PositionEntity(sp,vert\x,vert\y,vert\z)
      Next
      toffset+torsion
    Next
  EndIf
  For b=0 To lt-1
    For a=0 To l-1
      offset+1
      MP_AddTriangle(Mesh,offset+l+1,offset+l,offset)
      MP_AddTriangle(Mesh,offset+l,offset-1,offset)
    Next
    offset+1
  Next
  MP_EntitySetNormals(Mesh)
  ProcedureReturn Mesh
EndProcedure
Procedure.i Linea(x0.f=1.0,y0.f=1.0,z0.f=1.0,x1.f=0.0,y1.f=0.0,z1.f=0.0,color.l=$AA3E77BB)
  Protected celda.i=MP_CreatePrimitives(2,2)
  MP_SetPrimitives(celda,0,x0,y0,z0,color)
  MP_SetPrimitives(celda,1,x1,y1,z1,color)
  ProcedureReturn celda
EndProcedure
MP_PhysicInit()
; NewMaterial.i=MP_CreatePhysicMaterial()
; MP_SetPhysicMaterialProperties(NewMaterial,0.5,3,2.8)
;/ Luz
light.i=MP_CreateLight(1)
MP_LightSetColor(light,$aaaaaa)
MP_PositionEntity(light,4,4,0)
MP_CreateSkyBox("..\media\skyboxsun5deg","jpg",100)
;\
;/ cam
cam.i=MP_CreateCamera():pivotcam.i=MP_CreateMesh():MP_EntitySetParent(cam,pivotcam,0):MP_PositionEntity(cam,0,-2,-15)
;MP_TurnEntity(pivotcam,4,36,0,0)
;\
;/ fondo
fondo.i=MP_CreateRectangle(256,0,256)
MP_PositionEntity(fondo,0,-4.5,0)
fondotexture.i=MP_LoadTexture("..\media\wood-floor2.jpg",0,0)
MP_EntitySetTexture(fondo,fondotexture,0,0)
MP_EntityPhysicBody(fondo,1,50)
tex.i=MP_LoadTexture(#PB_Compiler_Home+"examples/3d/Data/Textures/MRAMOR6X6.jpg",0,0)
;\
aspectratio=1/4:ancho=4:largo=ancho/aspectratio:ancho_2=ancho/2:largo_2=largo/2
Esqueleto.i=MP_CreateCube():MP_ResizeMesh(Esqueleto,ancho,1,largo)
dist=0.1:fuerz=1
*rot.D3DMATRIX
sentido.Vector3D:pos.Vector3D
MP_MaterialDiffuseColor(Esqueleto,$aa,$BE,$DA,$DB)
MP_EntityPhysicBody(Esqueleto,4,100)
; MP_ConstraintCreateUpVector(Esqueleto,0,1,0)
;/ horquillas:
distanciaesqueletohorquilla=-1
Macro horquilla(ho,px,py,pz)
  horquilla#ho#=CreateTorusArc(10,20,0.1,0.1,0.4,#PI,0.0):MP_RotateMesh(horquilla#ho#,-90,0,0)
  p1.i=MP_CreateCylinder(0.1,1,0.1,10,1,0):MP_RotateMesh(p1,-90,0,0)
  MP_TranslateMesh(p1,0.4,-0.5,0)
  MP_AddMesh(p1,horquilla#ho#)
  MP_RotateMesh(p1,0,180,0)
  MP_AddMesh(p1,horquilla#ho#):MP_FreeEntity(p1)
  MP_PositionEntity(horquilla#ho#,px#,py#,pz#)
  MP_EntityPhysicBody(horquilla#ho#,4,20)
  MP_ConstraintCreateSlider(horquilla#ho#,0,-1,0,0,0,0,Esqueleto)
  MP_LimitSliderDistance(horquilla#ho#,0,0.01)
;   MP_ConstraintCreateHinge(horquilla#ho#,0,1,0,0,0,0,Esqueleto)
EndMacro
horquilla(DD.i,ancho_2,distanciaesqueletohorquilla,largo_2)
horquilla(DI.i,-ancho_2,distanciaesqueletohorquilla,largo_2)
horquilla(TD.i,ancho_2,distanciaesqueletohorquilla,-largo_2)
horquilla(TI.i,-ancho_2,distanciaesqueletohorquilla,-largo_2)
;\
;/ ruedas:
distanciaesqueletorueda=-2
Macro rueda(r,x,y,z)
  rueda#r#=MP_CreateCylinder(0.8,0.3,0.8,40,1,1):MP_RotateMesh(rueda#r#,0,-90,0)
  MP_EntitySetTexture(rueda#r#,tex,0,0)
  MP_PositionEntity(rueda#r#,x#,y#,z#)
  MP_EntityPhysicBody(rueda#r#,4,30)
  MP_ConstraintCreateHinge(rueda#r#,1,0,0,0,0,0,horquilla#r#)
  ;MP_SetPhysicMaterialtoMesh(rueda#r#,NewMaterial)
EndMacro
rueda(DD.i,ancho_2,distanciaesqueletorueda,largo_2)
rueda(DI.i,-ancho_2,distanciaesqueletorueda,largo_2)
rueda(TD.i,ancho_2,distanciaesqueletorueda,-largo_2)
rueda(TI.i,-ancho_2,distanciaesqueletorueda,-largo_2)
distanciahorquillarueda=distanciaesqueletorueda-distanciaesqueletohorquilla
;\
MP_MouseInWindow()
MP_UseCursor(0)
Macro ObtenerMatrizHorquilla(v)
  *rot.D3DMATRIX=MP_EntityGetMatrix(horquilla#v#)
  sentido\x=fuerz**rot\_31:sentido\y=fuerz**rot\_32:sentido\z=fuerz**rot\_33
  pos\x=*rot\_41:pos\y=*rot\_42:pos\z=*rot\_43
EndMacro
While MP_KeyDown(#PB_Key_Escape)=0 And WindowEvent()<>#PB_Event_CloseWindow
  mdx=MP_MouseDeltaX()/200:mdy=MP_MouseDeltaY()/200:mdw=MP_MouseDeltaWheel()/400
  If mdw
    ;MP_EntitySetZ(cam,MP_EntityGetZ(cam)+mdw)
    MP_MoveEntity(cam,0,0,mdw)
  ElseIf MP_MouseButtonDown(1)
    MP_MoveEntity(pivotcam,mdx,-mdy,0)
  ElseIf mdy Or mdx
    MP_TurnEntity(pivotcam,mdy*60,mdx*60,0,0)
  ElseIf MP_KeyDown(#PB_Key_Up)
    ObtenerMatrizHorquilla(DD.i)
    MP_EntityAddImpulse(ruedaDD,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaDD,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
    ObtenerMatrizHorquilla(DI.i)
    MP_EntityAddImpulse(ruedaDI,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaDI,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
;     ObtenerMatrizHorquilla(TD.i)
;     MP_EntityAddImpulse(ruedaTD,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaTD,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
;     ObtenerMatrizHorquilla(TI.i)
;     MP_EntityAddImpulse(ruedaTI,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaTI,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
  ElseIf MP_KeyDown(#PB_Key_Down)
    ObtenerMatrizHorquilla(DD.i)
    MP_EntityAddImpulse(ruedaDD,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaDD,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
    ObtenerMatrizHorquilla(DI.i)
    MP_EntityAddImpulse(ruedaDI,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaDI,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
;     ObtenerMatrizHorquilla(TD.i)
;     MP_EntityAddImpulse(ruedaTD,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaTD,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
;     ObtenerMatrizHorquilla(TI.i)
;     MP_EntityAddImpulse(ruedaTI,-sentido\x,-sentido\y,-sentido\z,pos\x,pos\y+distanciahorquillarueda+dist,pos\z);:MP_EntityAddImpulse(ruedaTI,sentido\x,sentido\y,sentido\z,pos\x,pos\y+distanciahorquillarueda-dist,pos\z)
  EndIf
  If MP_KeyDown(#PB_Key_Left)
    MP_TurnEntity(horquillaDD,0,-1,0):MP_TurnEntity(ruedaDD,0,-1,0)
    MP_TurnEntity(horquillaDI,0,-1,0):MP_TurnEntity(ruedaDI,0,-1,0)
  ElseIf MP_KeyDown(#PB_Key_Right)
    MP_TurnEntity(horquillaDD,0,1,0):MP_TurnEntity(ruedaDD,0,1,0)
    MP_TurnEntity(horquillaDI,0,1,0):MP_TurnEntity(ruedaDI,0,1,0)
  EndIf
  If MP_KeyDown(#PB_Key_O)
    coDD+0.1:If coDD>10:coDD=10:EndIf
    MP_LimitSliderDistance(horquillaDD,coDD,coDD+0.01)
;   ElseIf MP_KeyDown(#PB_Key_I)
    coDI+0.1:If coDI>10:coDI=10:EndIf
    MP_LimitSliderDistance(horquillaDI,coDI,coDI+0.01)
;   ElseIf MP_KeyDown(#PB_Key_L)
    coTD+0.1:If coTD>10:coTD=10:EndIf
    MP_LimitSliderDistance(horquillaTD,coTD,coTD+0.01)
;   ElseIf MP_KeyDown(#PB_Key_K)
    coTI+0.1:If coTI>10:coTI=10:EndIf
    MP_LimitSliderDistance(horquillaTI,coTI,coTI+0.01)
  ElseIf MP_KeyDown(#PB_Key_W)
    coDD-0.1:If coDD<0.01:coDD=0.01:EndIf
    MP_LimitSliderDistance(horquillaDD,coDD,coDD+0.01)
;   ElseIf MP_KeyDown(#PB_Key_q)
    coDI-0.1:If coDI<0.01:coDI=0.01:EndIf
    MP_LimitSliderDistance(horquillaDI,coDI,coDI+0.01)
;   ElseIf MP_KeyDown(#PB_Key_s)
    coTD-0.1:If coTD<0.01:coTD=0.01:EndIf
    MP_LimitSliderDistance(horquillaTD,coTD,coTD+0.01)
;   ElseIf MP_KeyDown(#PB_Key_a)
    coTI-0.1:If coTI<0.01:coTI=0.01:EndIf
    MP_LimitSliderDistance(horquillaTI,coTI,coTI+0.01)
  EndIf
;   If MP_KeyHit(#PB_Key_M); <- For debugging:
;     If lin.i:MP_FreeEntity(lin.i):EndIf
;     If vec.i:MP_FreeEntity(vec.i):EndIf
;     If lin2.i:MP_FreeEntity(lin2.i):EndIf
;     If vec2.i:MP_FreeEntity(vec2.i):EndIf
;     ObtenerMatrizHorquilla(DD.i)
;     lin.i=linea(pos\x,pos\y+distanciahorquillarueda+dist*40,pos\z,pos\x,pos\y+distanciahorquillarueda-dist*40,pos\z)
;     vec.i=linea(pos\x,pos\y+distanciahorquillarueda+dist*40,pos\z,pos\x+sentido\x,pos\y+distanciahorquillarueda+dist*40+sentido\y,pos\z+sentido\z)
;     ObtenerMatrizHorquilla(DI.i)
;     lin2.i=linea(pos\x,pos\y+distanciahorquillarueda+dist*40,pos\z,pos\x,pos\y+distanciahorquillarueda-dist*40,pos\z)
;     vec2.i=linea(pos\x,pos\y+distanciahorquillarueda+dist*40,pos\z,pos\x+sentido\x,pos\y+distanciahorquillarueda+dist*40+sentido\y,pos\z+sentido\z)
;   EndIf
  MP_PhysicUpdate()
  MP_RenderWorld()
  MP_Flip():Delay(8)
Wend
MP_PhysicEnd()
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: MP3D Engine Alpha 32

Post by applePi »

@Psychophanta, this car is good, haven't noticed your latest post, thinking it is the older post. can't resist praising your work before looking at it, thanks. i will experiment with it.
@Michael: it seems this function MP_EntityCollTri can be used to disable collision by setting the triangles dedicated for collision to zero, in the spirit of

Code: Select all

SetEntityAttribute(#spring, #PB_Entity_DisableContactResponse, 1)
i may misunderstood its purpose.
but when used it in a program like MP_Box Box Kollison.pb from the mp3d examples
like adding this:
MP_EntityCollTri(Mesh1, 0 )
before this:
If MP_3DCollisionCube2Cube (Mesh1,Mesh2)
we get memory error
i may use it wrongly
Post Reply