change sprite3d offset?

Advanced game related topics
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

change sprite3d offset?

Post by superadnim »

I'd like to change the center point of my sprite3d, so I can correct my rotation, the offset should be set to minus 10 pixels... I can't seem to get around this one with sprite3d lib, perhaps I should use the transformation function?, any examples/ideas?

Thanks

:lol: should I bash the keyboard and give up?
:?
Anonymous

Post by Anonymous »

Try this :

Code: Select all

Structure vertex
  sx.f
  sy.f
  sz.f
  rhw.f
  Color.l
  specular.l
  tu.f
  tv.f
EndStructure

Structure PB_Sprite3D
  Texture.l         
  Vertice.vertex[4]
  Width.w
  Height.w
EndStructure

Structure PB_Sprite3DEX
  No.l
  AA.f:AB.f:AC.f:AD.f
  DA.f:DB.f:DC.f:dd.f
  Pivot_x.f
  Pivot_y.f
EndStructure





Procedure Distance(x1,y1,x2,y2)
  Protected Result.f
  Result = Sqr(  Pow(x1-x2,2) + Pow(y1-y2,2) + Pow(z1-z2,2) )
  ProcedureReturn Result
EndProcedure


Procedure.f ReturnAngle(x1.f,y1.f,x2.f,y2.f)
  A.f = x1-x2
  b.f = y1-y2
  c.f = -Sqr(A*A+b*b)
  angle.f = ACos(A/c)*180/#PI
  If y1 < y2 : angle=360-angle : EndIf
  ProcedureReturn Abs(angle - 360)
EndProcedure


Procedure Sprite3DModifyPivot(*SpriteID.PB_Sprite3DEX,X.f,Y.f)
  Protected *VertexSprite.PB_Sprite3D = IsSprite3D(*SpriteID\No)
  
  *SpriteID\AA = ReturnAngle(*VertexSprite\Vertice[0]\sx,*VertexSprite\Vertice[0]\sy,X,Y)
  *SpriteID\AB = ReturnAngle(*VertexSprite\Vertice[1]\sx,*VertexSprite\Vertice[1]\sy,X,Y)
  *SpriteID\AC = ReturnAngle(*VertexSprite\Vertice[2]\sx,*VertexSprite\Vertice[2]\sy,X,Y)
  *SpriteID\AD = ReturnAngle(*VertexSprite\Vertice[3]\sx,*VertexSprite\Vertice[3]\sy,X,Y)
  
  *SpriteID\DA = Distance(*VertexSprite\Vertice[0]\sx,*VertexSprite\Vertice[0]\sy,X,Y)
  *SpriteID\DB = Distance(*VertexSprite\Vertice[1]\sx,*VertexSprite\Vertice[1]\sy,X,Y)
  *SpriteID\DC = Distance(*VertexSprite\Vertice[2]\sx,*VertexSprite\Vertice[2]\sy,X,Y)
  *SpriteID\dd = Distance(*VertexSprite\Vertice[3]\sx,*VertexSprite\Vertice[3]\sy,X,Y)
  
  *SpriteID\Pivot_x=X
  *SpriteID\Pivot_y=Y
EndProcedure

Procedure CreateSprite3DEX(Sprite3D)
  Protected *VertexSprite.PB_Sprite3D = IsSprite3D(Sprite3D)
  Protected *VertexSpriteEX.PB_Sprite3DEX = AllocateMemory(SizeOf(PB_Sprite3DEX))
  
  *VertexSpriteEX\No = Sprite3D
  
  
  
  SizeW.f = *VertexSprite\Width/2
  SizeH.f = *VertexSprite\Height/2
  
  
  *VertexSpriteEX\AA = ReturnAngle(*VertexSprite\Vertice[0]\sx,*VertexSprite\Vertice[0]\sy,SizeW,SizeH)
  *VertexSpriteEX\AB = ReturnAngle(*VertexSprite\Vertice[1]\sx,*VertexSprite\Vertice[1]\sy,SizeW,SizeH)
  *VertexSpriteEX\AC = ReturnAngle(*VertexSprite\Vertice[2]\sx,*VertexSprite\Vertice[2]\sy,SizeW,SizeH)
  *VertexSpriteEX\AD = ReturnAngle(*VertexSprite\Vertice[3]\sx,*VertexSprite\Vertice[3]\sy,SizeW,SizeH)
  
  *VertexSpriteEX\DA = Distance(*VertexSprite\Vertice[0]\sx,*VertexSprite\Vertice[0]\sy,SizeW,SizeH)
  *VertexSpriteEX\DB = Distance(*VertexSprite\Vertice[1]\sx,*VertexSprite\Vertice[1]\sy,SizeW,SizeH)
  *VertexSpriteEX\DC = Distance(*VertexSprite\Vertice[2]\sx,*VertexSprite\Vertice[2]\sy,SizeW,SizeH)
  *VertexSpriteEX\dd = Distance(*VertexSprite\Vertice[3]\sx,*VertexSprite\Vertice[3]\sy,SizeW,SizeH)
  
  ProcedureReturn *VertexSpriteEX
EndProcedure


Procedure RotateSprite3DEX(*SpriteID.PB_Sprite3DEX,angle.f)
  Protected *VertexSprite.PB_Sprite3D = IsSprite3D(*SpriteID\No)
  Protected SizeW2.l,SizeH2.l
  If angle = 0 Or angle = 180
    *SpriteID\Pivot_x = *VertexSprite\Width/2
    *SpriteID\Pivot_y = *VertexSprite\Height/2
  Else
    *SpriteID\Pivot_x = *VertexSprite\Height/2
    *SpriteID\Pivot_y = *VertexSprite\Width/2
  EndIf
  *VertexSprite\Vertice[0]\sx = *SpriteID\Pivot_x  - *SpriteID\DA  * Cos((angle+*SpriteID\AA)*#PI/180)
  *VertexSprite\Vertice[0]\sy = *SpriteID\Pivot_y  - *SpriteID\DA  * Sin((angle+*SpriteID\AA)*#PI/180)
  
  *VertexSprite\Vertice[1]\sx = *SpriteID\Pivot_x  - *SpriteID\DB  * Cos((angle+*SpriteID\AB)*#PI/180)
  *VertexSprite\Vertice[1]\sy = *SpriteID\Pivot_y  - *SpriteID\DB  * Sin((angle+*SpriteID\AB)*#PI/180)
  ; ;                                         
  *VertexSprite\Vertice[3]\sx = *SpriteID\Pivot_x  - *SpriteID\dd  * Cos((angle+*SpriteID\AD)*#PI/180)
  *VertexSprite\Vertice[3]\sy = *SpriteID\Pivot_y  - *SpriteID\dd  * Sin((angle+*SpriteID\AD)*#PI/180)
  
  *VertexSprite\Vertice[2]\sx = *SpriteID\Pivot_x  - *SpriteID\DC  * Cos((angle+*SpriteID\AC)*#PI/180)     
  *VertexSprite\Vertice[2]\sy = *SpriteID\Pivot_y  - *SpriteID\DC  * Sin((angle+*SpriteID\AC)*#PI/180)
  
EndProcedure



InitSprite() : InitSprite3D() : InitKeyboard()
If OpenWindow(0, 0, 0, 800, 600, "Un écran dans une fenêtre...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)
  Else
    MessageRequester("Erreur", "Impossible d'ouvrir un écran dans la fenêtre!", 0)
    End
  EndIf
EndIf

;{ A
CreateSprite(0,128,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,RGB(255,0,0))
Box(64,0,64,64,RGB(255,255,0))
Box(0,0,10,10, $FF0000)
StopDrawing()
CreateSprite3D(0,0)
A = CreateSprite3DEX(0)
;}
;{ B
CreateSprite(1,128,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(1))
Box(0,0,64,64,RGB(255,0,0))
Box(64,0,64,64,RGB(255,255,0))
Box(0,54,10,10, $FF0000)
StopDrawing()
CreateSprite3D(1,1)
b = CreateSprite3DEX(1)
;}
;{ C
CreateSprite(2,128,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(2))
Box(0,0,64,64,RGB(255,0,0))
Box(64,0,64,64,RGB(255,255,0))
Box(118,54,10,10, $FF0000)
StopDrawing()
CreateSprite3D(2,2)
c = CreateSprite3DEX(2)
;}
;{ D
CreateSprite(3,128,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(3))
Box(0,0,64,64,RGB(255,0,0))
Box(64,0,64,64,RGB(255,255,0))
Box(118,0,10,10, $FF0000)
StopDrawing()
CreateSprite3D(3,3)
d = CreateSprite3DEX(3)
;}
e = CreateSprite3DEX(0)
f = CreateSprite3DEX(0)



Sprite3DModifyPivot(e,0,32)
Sprite3DModifyPivot(f,64,0)


Repeat
  r.f = 90 + 45 * Cos(ElapsedMilliseconds()/500)
  ExamineKeyboard()
  ClearScreen(0)
  Start3D()
  RotateSprite3DEX(A,0)
  DisplaySprite3D(0,100,100)
  
  RotateSprite3DEX(b,90)
  DisplaySprite3D(1,300,100)
  
  RotateSprite3DEX(c,180)
  DisplaySprite3D(2,100,300)
  
  RotateSprite3DEX(d,270)
  DisplaySprite3D(3,300,300)
  
  RotateSprite3DEX(e,r)
  DisplaySprite3D(0,500,200)
  
  
  RotateSprite3DEX(f,-r)
  DisplaySprite3D(0,500,400)
  Stop3D()
  
  StartDrawing(ScreenOutput())
  DrawText(100,100,"0 DEG")
  DrawText(300,100,"90 DEG")
  DrawText(100,300,"180 DEG")
  DrawText(300,300,"270 DEG")
  Circle(500,232,4,RGB(0,0,255))
  Circle(564,400,4,RGB(0,0,255))
  Box(100,100,10,10, $00FF00)
  Box(300,100,10,10, $00FF00)
  Box(100,300,10,10, $00FF00)
  Box(300,300,10,10, $00FF00)
  StopDrawing()
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) 
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

@ Bator: NICE!!! 8)

One thing: When I accidentlyy clicked the mouse inside the window it forced the window to WHITE color and stopped the program.
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
hellhound66
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Feb 21, 2006 12:37 pm

Post by hellhound66 »

Removed.
Last edited by hellhound66 on Wed Mar 19, 2008 11:38 pm, edited 1 time in total.
dige
Addict
Addict
Posts: 1430
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

@Cpl.Bator: thanks a lot!
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post by superadnim »

Thanks :)

Either way I'm not using PB anymore for this, it obviously lacks it all and I can't be spending time on "writing my own" when all I want is to focus on the game itself.

Theres clearly no proper libraries whatsoever when it comes to game-dev in PB, mainly - it could be - due to lack of experience on the field from those who actually wrote whatever is available now adays... Either way I would rather keep on working in C# and SlimDX than pulling my hair out with 10 year old libs that can't even handle proper rotation and are state-less implementations.

:lol:

PS: My solution was to rotate the point around another point, thus effectively changing the rotation origin with my own displacement (this was needed for a turret rotation, where I didn't want to waste resources using bigger sprites). Oh well...

:lol: should I bash the keyboard and give up?
:?
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

as a 'superadnim' , you're better in c#. goodluck ;)
SPAMINATOR NR.1
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

heehee
oh... and have a nice day.
Post Reply