Any way to AttachEntityObject() avoiding rotation

Everything related to 3D programming
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Any way to AttachEntityObject() avoiding rotation

Post by Psychophanta »

I am trying to do an explanation in screen of some physic bodies behaviour, and i implemented 3d arrows and pointing loops to specify turns in the space.
Some arrows must follow the body center of masses linear displacement, but not to rotate with it.
So I am looking for AttachEntityObject() but only for the movement of the body and not for its rotations.
Is there a way or trick to do that without the use of joints?
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Any way to AttachEntityObject() avoiding rotation

Post by Comtois »

not sure what you are looking for, so here 2 examples

Code: Select all

#CameraSpeed = 2

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Structure VECTOR3
  x.f
  y.f
  z.f
EndStructure

Define.f KeyX, KeyY, MouseX, MouseY, d = 20
Define.VECTOR3 x,y,z

Declare Normalize(*V.VECTOR3)
Declare ProduitVectoriel(*N.VECTOR3, *V1.VECTOR3, *V2.VECTOR3)

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/Desert.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    ; First create materials
    ;
    
    GrassMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"grass1.png")))
    MaterialBlendingMode(GrassMaterial, #PB_Material_AlphaBlend)
    DirtMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"Dirt.jpg")))
    NinjaRed = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"nskinrd.jpg")))
    Red = GetScriptMaterial(#PB_Any, "Color/Red")
    Blue = GetScriptMaterial(#PB_Any, "Color/Blue")
    Green = GetScriptMaterial(#PB_Any, "Color/Green")
    
    ; Then create the billboard group and use the previous material
    ;
    ;-Billboard
    
    Billboard = CreateBillboardGroup(#PB_Any, MaterialID(GrassMaterial), 60, 60)
    For i = 0 To 1000
      AddBillboard(Billboard, Random(2000)-1000, 30, Random(2000) - 1000)
    Next i
    
    ; create ground
    MeshPlane = CreatePlane(#PB_Any, 2000, 2000, 40, 40, 4, 4)
    CreateEntity(#PB_Any, MeshID(MeshPlane), MaterialID(DirtMaterial))
    
    ; Add house
    MeshHouse = LoadMesh(#PB_Any, "tudorhouse.mesh")
    House = CreateEntity(#PB_Any, MeshID(MeshHouse), #PB_Material_None, 0, 280, 0)
    ScaleEntity(House, 0.5, 0.5, 0.5)
    
    ;- Ninja
    NinjaMesh = LoadMesh(#PB_Any, "ninja.mesh")
    Ninja = CreateEntity(#PB_Any, MeshID(NinjaMesh), MaterialID(NinjaRed), 500, 0, 400)
    ScaleEntity(Ninja, 0.5, 0.5, 0.5)  
    StartEntityAnimation(Ninja, "Walk")
    
    ;Sphere 
    Sphere = CreateSphere(#PB_Any, 5)
    SphereUp = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Red), 0, 110, 0)   
    SphereLeft = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Green), -d, 55, 0)
    SphereRight = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Blue),  d, 55, 0)
    
    CreateNode(0)
    AttachNodeObject(0, EntityID(SphereUp))
    AttachNodeObject(0, EntityID(SphereLeft))
    AttachNodeObject(0, EntityID(SphereRight))
    
    ; SkyBox
    SkyBox("Desert07.jpg")
    
    ; create camera
    Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
    
    
    ; Light
    CreateLight(0, RGB(255, 255, 255), 1560, 900, 500)
    AmbientColor(RGB(50,50,50))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.03
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX * 0.85
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY * 0.9
        EndIf
        
      EndIf
      
      MoveEntity(Ninja, KeyX, 0, KeyY, #PB_Local)
      Yaw(EntityID(Ninja), MouseX, #PB_World)         
      CameraFollow(Camera, EntityID(Ninja), 0, EntityY(Ninja) + 80, 200, 0.1, 0.1, #False)
      CameraLookAt(Camera, EntityX(Ninja), 40, EntityZ(Ninja))      
      
      
      MoveNode(0, EntityX(Ninja), EntityY(Ninja),EntityZ(Ninja), #PB_Absolute) 
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Code: Select all

#CameraSpeed = 2

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Structure VECTOR3
  x.f
  y.f
  z.f
EndStructure

Define.f KeyX, KeyY, MouseX, MouseY, d = 20
Define.VECTOR3 x,y,z

Declare Normalize(*V.VECTOR3)
Declare ProduitVectoriel(*N.VECTOR3, *V1.VECTOR3, *V2.VECTOR3)

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Models", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
    Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Packs/Desert.zip", #PB_3DArchive_Zip)
    Parse3DScripts()
    
    ; First create materials
    ;
    
    GrassMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"grass1.png")))
    MaterialBlendingMode(GrassMaterial, #PB_Material_AlphaBlend)
    DirtMaterial = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"Dirt.jpg")))
    NinjaRed = CreateMaterial(#PB_Any, TextureID(LoadTexture(#PB_Any,"nskinrd.jpg")))
    Red = GetScriptMaterial(#PB_Any, "Color/Red")
    Blue = GetScriptMaterial(#PB_Any, "Color/Blue")
    Green = GetScriptMaterial(#PB_Any, "Color/Green")
    
    ; Then create the billboard group and use the previous material
    ;
    ;-Billboard
    
    Billboard = CreateBillboardGroup(#PB_Any, MaterialID(GrassMaterial), 60, 60)
    For i = 0 To 1000
      AddBillboard(Billboard, Random(2000)-1000, 30, Random(2000) - 1000)
    Next i
    
    ; create ground
    MeshPlane = CreatePlane(#PB_Any, 2000, 2000, 40, 40, 4, 4)
    CreateEntity(#PB_Any, MeshID(MeshPlane), MaterialID(DirtMaterial))
    
    ; Add house
    MeshHouse = LoadMesh(#PB_Any, "tudorhouse.mesh")
    House = CreateEntity(#PB_Any, MeshID(MeshHouse), #PB_Material_None, 0, 280, 0)
    ScaleEntity(House, 0.5, 0.5, 0.5)
    
    ;- Ninja
    NinjaMesh = LoadMesh(#PB_Any, "ninja.mesh")
    Ninja = CreateEntity(#PB_Any, MeshID(NinjaMesh), MaterialID(NinjaRed), 500, 0, 400)
    ScaleEntity(Ninja, 0.5, 0.5, 0.5)  
    StartEntityAnimation(Ninja, "Walk")
    
    ;Sphere 
    Sphere = CreateSphere(#PB_Any, 5)
    SphereUp = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Red))
    SphereLeft = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Green))
    SphereRight = CreateEntity(#PB_Any, MeshID(Sphere), MaterialID(Blue))
        
    ; SkyBox
    SkyBox("Desert07.jpg")
    
    ; create camera
    Camera = CreateCamera(#PB_Any, 0, 0, 100, 100)
    
    
    ; Light
    CreateLight(0, RGB(255, 255, 255), 1560, 900, 500)
    AmbientColor(RGB(50,50,50))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.03
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX * 0.85
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY * 0.9
        EndIf
        
      EndIf
      
      MoveEntity(Ninja, KeyX, 0, KeyY, #PB_Local)
      Yaw(EntityID(Ninja), MouseX, #PB_World)         
      CameraFollow(Camera, EntityID(Ninja), 0, EntityY(Ninja) + 80, 200, 0.1, 0.1, #False)
      CameraLookAt(Camera, EntityX(Ninja), 40, EntityZ(Ninja))      
      
      ; Get camera's axis
      y\x = 0
      y\y = 1
      y\z = 0
      z\x = CameraDirectionX(Camera)
      z\y = CameraDirectionY(Camera)
      z\z = CameraDirectionZ(Camera)
      
      ProduitVectoriel(@x, @z, @y)
      Normalize(@x)
      
      ; Spheres
      MoveEntity(SphereUp   , EntityX(Ninja), EntityY(Ninja) + 110, EntityZ(Ninja), #PB_Absolute) 
      MoveEntity(SphereLeft , EntityX(Ninja) - x\x * d, EntityY(Ninja) +  55 - x\y * d, EntityZ(Ninja) - x\z * d, #PB_Absolute) 
      MoveEntity(SphereRight, EntityX(Ninja) + x\x * d, EntityY(Ninja) +  55 + x\y * d, EntityZ(Ninja) + x\z * d, #PB_Absolute) 
      
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized",0)
EndIf

End

Procedure Normalize(*V.VECTOR3)
  Define.f magSq, oneOverMag
  
  magSq = *V\x * *V\x + *V\y * *V\y + *V\z * *V\z
  If magsq > 0
    oneOverMag = 1.0 / Sqr(magSq)
    *V\x * oneOverMag
    *V\y * oneOverMag
    *V\z * oneOverMag
  EndIf 
  
EndProcedure

Procedure ProduitVectoriel(*N.VECTOR3, *V1.VECTOR3, *V2.VECTOR3)
  *N\x = *V1\y * *V2\z - *V1\z * *V2\y
  *N\y = *V1\z * *V2\x - *V1\x * *V2\z
  *N\z = *V1\x * *V2\y - *V1\y * *V2\x
EndProcedure
Please correct my english
http://purebasic.developpez.com/
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Any way to AttachEntityObject() avoiding rotation

Post by Psychophanta »

That's not.
Run this.
After collision, the bar rotates and translates.
I added an arrow entity in the center of the bar, and attached the arrow to the bar, but i want the arrow displayed in the center of the bar and translate with the bar, but not rotation for the arrow.

Code: Select all

ExamineDesktops()
bitplanes.a=DesktopDepth(0):RX.u=DesktopWidth(0):RY.u=DesktopHeight(0)
InitEngine3D()
InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,RX,RY,"tip",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,0,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

luz.i=CreateLight(#PB_Any,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(luz.i,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(luz,4,4,2,#PB_Absolute)
camara.i=CreateCamera(#PB_Any,0,0,100,100):MoveCamera(camara.i,0,0,3,#PB_Absolute)
cuerpotextura.i=LoadTexture(#PB_Any,"soil_wall.jpg")
cuerpomaterial.i=CreateMaterial(#PB_Any,TextureID(cuerpotextura.i))
cuerpomalla.i=CreateCylinder(#PB_Any,0.05,2,10,1,1)
cuerpo.i=CreateEntity(#PB_Any,MeshID(cuerpomalla.i),MaterialID(cuerpomaterial.i),0,0,0)

bolatextura.i=LoadTexture(#PB_Any,"snow_1024.jpg")
bolamaterial.i=CreateMaterial(#PB_Any,TextureID(bolatextura.i))
bolamalla.i=CreateSphere(#PB_Any,0.05,25,10)
bola.i=CreateEntity(#PB_Any,MeshID(bolamalla.i),MaterialID(bolamaterial.i),-0.3,0.8,0)

WorldGravity(0.0)
CreateEntityBody(cuerpo,#PB_Entity_CylinderBody,1.0,1.0,0.0):SetEntityAttribute(cuerpo,#PB_Entity_LinearSleeping,0.0):SetEntityAttribute(cuerpo,#PB_Entity_AngularSleeping,0.0)
CreateEntityBody(bola,#PB_Entity_SphereBody,1.0,1.0,0.0):SetEntityAttribute(bola,#PB_Entity_LinearSleeping,0.0):SetEntityAttribute(bola,#PB_Entity_AngularSleeping,0.0)
Repeat
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Space)
    ApplyEntityImpulse(bola.i,0.3,0,0,0,0,0)
  EndIf
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
CloseWindow(0)
I know how to do it, but the question is if there is a trick to do it attaching arrow to the bar, i.e. without the use of commands in the main loop.
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: Any way to AttachEntityObject() avoiding rotation

Post by applePi »

so the arrow should translate with its mother the bar, but the arrow must not rotate with its mother.
i think the solution is to let the arrow to look at very far location by using EntityLookAt(x.y.z)
without the use of commands in the main loop
not possible since EntityLookAt(x.y.z) should adjust itself continuously while it is translating to another place
EntityLookAt(arrow, 0, -1000, 0) : will look at a place far away, how much the far away? depends on the dimensions of your model.
the arrow here replaced by a cube. i guess but not sure that triangle number 1 is the face used to perform the process of "look at". so make the arrow upon this idea.

Code: Select all

ExamineDesktops()
bitplanes.a=DesktopDepth(0):RX.u=DesktopWidth(0):RY.u=DesktopHeight(0)
InitEngine3D()
InitSprite():InitKeyboard():InitMouse()
OpenWindow(0,0,0,RX,RY,"tip",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,RX,RY,0,0,0,#PB_Screen_WaitSynchronization)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

luz.i=CreateLight(#PB_Any,$EEEEEE,4,4,2,#PB_Light_Point):SetLightColor(luz.i,#PB_Light_DiffuseColor,$EEEEEE):MoveLight(luz,4,4,2,#PB_Absolute)
camara.i=CreateCamera(#PB_Any,0,0,100,100):MoveCamera(camara.i,0,0,3,#PB_Absolute)
cuerpotextura.i=LoadTexture(#PB_Any,"soil_wall.jpg")
cuerpomaterial.i=CreateMaterial(#PB_Any,TextureID(cuerpotextura.i))
cuerpomalla.i=CreateCylinder(#PB_Any,0.05,2,10,1,1)
rod.i=CreateEntity(#PB_Any,MeshID(cuerpomalla.i),MaterialID(cuerpomaterial.i),0,0,0)

bolatextura.i=LoadTexture(#PB_Any,"snow_1024.jpg")
bolamaterial.i=CreateMaterial(#PB_Any,TextureID(bolatextura.i))
bolamalla.i=CreateSphere(#PB_Any,0.05,25,10)
bola.i=CreateEntity(#PB_Any,MeshID(bolamalla.i),MaterialID(bolamaterial.i),-0.3,0.8,0)

tex=LoadTexture(#PB_Any,"Geebee2.bmp")
texture=CreateMaterial(#PB_Any,TextureID(tex))
cone = CreateCube(#PB_Any, 0.2)
arrow = CreateEntity(#PB_Any,MeshID(cone),MaterialID(texture),0,0,0.2)
AttachEntityObject(rod, "", EntityID(arrow))
;EntityLookAt(arrow, 0, 0,0)

WorldGravity(0.0)
CreateEntityBody(rod,#PB_Entity_CylinderBody,1.0,1.0,0.0):SetEntityAttribute(rod,#PB_Entity_LinearSleeping,0.0):SetEntityAttribute(rod,#PB_Entity_AngularSleeping,0.0)
CreateEntityBody(bola,#PB_Entity_SphereBody,1.0,1.0,0.0):SetEntityAttribute(bola,#PB_Entity_LinearSleeping,0.0):SetEntityAttribute(bola,#PB_Entity_AngularSleeping,0.0)
Repeat
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_Space)
    ApplyEntityImpulse(bola.i,0.3,0,0,0,0,0)
  EndIf
  EntityLookAt(arrow, 0, -1000, 0)
    
  RenderWorld()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)
CloseWindow(0)
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Any way to AttachEntityObject() avoiding rotation

Post by Psychophanta »

That is the effect i need, but without command in the loop, if possible.
Thanks anyway applePi.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply