Good day!
I load images (*.png) with native sizes (Size := Vector2(0, 0)).
How can I get their width and height in pixels?
(I need this information for sprites alignment on the scene)
You can look at LocalBoundingBox of the TCastleImageTransform instance (defined for every TCastleTransform).
When you left the Size as zero, then the LocalBoundingBox will reflect exactly the loaded image size. So, something like this should be OK:
var
Box: TBox3D;
begin
Box := MyImageTransform.LocalBoundingBox;
ImageWidth := Boxes.SizeX;
ImageHeight := Boxes.SizeY;
// .. do what you need with ImageWidth, ImageHeight
end;
Notes:
Boxes.SizeXandBoxes.SizeYareSingle. If you need to act on integers, then in this case it is OK to just round them, i.e.ImageWidth := Round(Boxes.SizeX). It will be OK, the underlying numbers are really integers in this case.- While this is efficient and universal answer, I understand it is not obvious
Let me know if this very counter-intuitive. Internally, we know the real image size, and we could add to TCastleImageTransformproperties likeImageWidth, ImageHeight:Cardinalthat would maybe be more straightforward to use. Comments welcome.
Nevermind, I decided myself that TCastleImageTransform.ImageWidth and TCastleImageTransform.ImageHeight would be more intuitive. They would make answer to this question more obvious
So, added them to CGE: Add TCastleImageTransform.ImageWidth/Height · castle-engine/castle-engine@f287d3f · GitHub .
Thanks, it works fine.
But I still have small problem. I making 2.5D game and use 2D sprites in 3D. So, if two NPC or monsters, for example, have different sizes, I need set their Translation.Y as zero plus half of their heights. In this case they will stand at one line.
Next, sizes is zero, but all sprites are scaled to correct displaying in 3D viewport (so, Scale is about 0.05… It is important addition I forgot to say).
It means, when I use Translation.Y = 0 + Box.SizeY / 2, very small (scaled) sprite get Translation.Y more then 100 and ups to the sky
![]()
What can you recommend in this case? How can I set Translation in same measures as scaled ImageTransform? I tried to find the multiplier empirically but failed ![]()
I recommend to create a hierarchy of transformations in this case. Basically, you don’t have to specify all translations/rotations/scaling on one TCastleTransform instance ( TCastleImageTransform is also TCastleTransform instance).
E.g.
- MyImageTransform (
TCastleImageTransform):- leave Scale as default (1 1 1)
- and change only
Translation, e.g. setMyImageTransform.TranslationtoVector3(0, -MyImageTransform.ImageHeight / 2, 0).
- Make
MyImageTransforma child ofMyScaledSprite(TCastleTransform)- and on
MyScaledSpriteset non-default scale likeVector3(0.1,0.1,0.1)if need.
- and on
Great, thanks! It is fixed, finally ![]()