Ooops
January 15, 2021, 05:52:34 PM

Author Topic: [bb] positionsprite() by D4NM4N [ 1+ years ago ]  (Read 400 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] positionsprite() by D4NM4N [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : positionsprite()
Author : D4NM4N
Posted : 1+ years ago

Description : This may be of use to someone,

its a simple utility to convert 2d coords back in to a virtual 2d screen for using sprites or other flat entities.
to test it, use a sprite with a cross in the center to see exactly where plotted.

I have seen several other ways of doing this, and although brilliant, are hard to trace through, so ive done this to keep uneccasary maths to a minimum and make it easy to follow.

the sprites are approx 3bu in front of the camera, so people who use insanely small scales may want to set entityorder to -1 for the sprites.

;possible uses:
;    HUD
;   lens flares
;   displaying 'lives' etc


Code :
Code: BlitzBasic
  1. ;--------------------------------------------------------------------------------
  2. ;A couple of functions to display 3d sprites using 2d coordinates!!
  3. ;By dan@d-grafix  
  4. ;--------------------------------------------------------------------------------
  5. ;firstly ive kept it as simple as possible.
  6. ;im not going in to the calculations involved to work out the cam dist and scale factors, but this is a
  7. ;simple utility to convert 2d coords back in to a virtual 2d screen for using sprites or other flat entities.
  8.  
  9. ;Positionsprite() - a simple Virtual 2d screen of 640x480 coordinate system for sprites. (works in any screen mode with these numbers)
  10. ;useprojectedx/y#() - a routne to convert position returned from projectedx/y() for higher screen resolutions
  11.  
  12. ;use:
  13. ;       positionsprite (entity, x(as 2d), y(as 2d), scale(1 keeps original size (256x256) for actual screen coords))
  14. ;       useprojectedx/y# (coords# (coordinate for conversion))
  15.  
  16. ;possible uses:
  17. ;       HUD
  18. ;       lens flares
  19. ;       displaying 'lives' etc
  20.  
  21. ;rules:
  22. ;       sprites must be parented to the primary camera (with normal zoom?(not tested for this))
  23. ;       screen modes must be equally proporsonate to 640x480... ie 320x240 1024x768 etc otherwise the you may loose some Y!
  24. ;       its not 100% accurate and (may) produce funny results when tiled sprite are used although this may be fixable with some fiddling
  25. ;       the scale factor of 1.0   is calculated from sprites loaded at 256x256. for smaller ones adjust it as appropriate
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. ;--------------------------------------------------------------------------------
  34. ;gfx setup
  35. ;--------------------------------------------------------------------------------
  36. ;try it with various screen modes
  37. Graphics3D 1024,768,16,1
  38.  
  39. Global gwidth                   :gwidth  =GraphicsWidth()
  40. Global gheight                  :gheight =GraphicsHeight()
  41.  
  42.  
  43. cam=CreateCamera()
  44. sprite1=LoadSprite("    your sprite.png      ",1,cam)
  45. sprite2=CopyEntity(sprite1,cam)
  46. sprite3=CopyEntity(sprite1,cam)
  47.  
  48.  
  49.  
  50. ;--------------------------------------------------------------------------------
  51. ;demo code
  52. ;--------------------------------------------------------------------------------
  53.         scale#=1
  54.        
  55.         ;position some sprites using virtual 640x480 coords (works in all relative screen modes)
  56.         positionsprite (sprite1,0,0,scale)              ;prints in top corner
  57.         positionsprite (sprite2,639,479,scale)  ; prints in bottom corner (-1 to show gren cross)
  58.        
  59.         ;position sprite using true screen coords (useful for use with projectedx/y() in different res modes ie. for lens flares)
  60.         px#=useprojectedx#(512)
  61.         py#=useprojectedy#(384)
  62.         positionsprite (sprite3,px#,py#,scale) ; prints at actual screen coords for grafx mode!
  63.  
  64.         RenderWorld()
  65.  
  66.         ;draw some 2d text guides to see whats happening
  67.         Text 50,10,"Virtual coord = 0,0"
  68.         Text gwidth-200,gheight-20,"Virtual coord = 640,480"
  69.         Text 512,384,"real screen coord = 512,384";
  70.        
  71.         Flip
  72.  
  73.         waitkey()
  74.  
  75.  
  76. ;--------------------------------------------------------------------------------
  77. ;projection coord converters
  78. ;--------------------------------------------------------------------------------
  79. Function useprojectedx#(coord#)
  80.         div#=640/Float(gwidth)
  81.         Return coord*div
  82. End Function
  83.  
  84. Function useprojectedy#(coord#)
  85.         div#=480/Float(gheight)
  86.         Return coord*div
  87. End Function
  88.  
  89. ;--------------------------------------------------------------------------------
  90. ;Position sprite on ' virtual 2d '  3d screen
  91. ;--------------------------------------------------------------------------------
  92. Function positionsprite(sprite,x#,y#,sps#=1.0,zdi#=3.2);zdi is precalculated and should not be changed (5.1 for upgrade to 1024)
  93.         sps = sps*1.28  ; 1.29 is precalculated too (best leave)
  94.         cx#=(x-(640*.5))*.01
  95.         cy#=(y-(480*.5))*.01
  96.         ScaleSprite sprite,sps,sps
  97.         PositionEntity sprite,cx,-cy,zdi
  98. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal