Get Character's current Animation-Frame's Size & Position?

  • #1, by caligarimarteMonday, 25. March 2019, 17:40 5 years ago
    Hello, I have recently experimented with some Shader-Ideas (Shadowcasting, Reflections, Crepuscular Rays) by loading my Character's current Sprite into different Shaders via "graphics.getCurrentSpritePath()" -- and it works pretty well! But I have only managed to approximately place and size the loaded Sprite manually inside the Shader, the manual Positioning does not always entirely match up. It would be nice if I could load in that current Animation-Frame's Size and Position (on Screen) to directly feed these Values into the Shader, automatically and always fitting. I have tried several Methods, among them "CurrentSpritePos", "Sprite:getPosition()", and "getAnimationSize()", but the Problem seems to be that the Methods either only work for Object-Sprites but not Character-Sprites, and/or I do not know how a Character's current Animation-Frame could be accessed (except for its Filepath). Can anyone help me with that?

    Forum Fan

    145 Posts


  • #2, by MachtnixTuesday, 26. March 2019, 01:10 5 years ago
    I don't understand your problem. My English isn't the best. What ist the difference between the character sprite's position and the character object's position?  I don't use shaders, so it may be a difference. My characters are 2D PNG-sprites (no 3D body) too.

    To request the character's position I use the simple get. It would be complicated, but it works for me and I will never change a running system... wink, because my scripting is inferior than my English.

    Obj_Elfe = getObject("Characters[Elfe]") -- hol Character und mache ein Objekt draus
    
    Pos_Obj_Elfe = getObject("Characters[Elfe]"):getPoint(VCharacterPosition)
    
    x_aktiv = Pos_Obj_Elfe.x --x-Position der aktiven Spielfigur
    y_aktiv = Pos_Obj_Elfe.y

    If I totally misunderstand the problem - please forget...

    Thread Captain

    1097 Posts

  • #3, by caligarimarteTuesday, 26. March 2019, 10:46 5 years ago
    Ah yes, it might make Sense to explain that Problem: The Character.Position is centered on the manually set Point where the Character's Feet touch the Ground, but if a Sprite is being loaded into a Shader as a Texture, the Center-Point will be the upper left Corner of the Sprite, so the Character-Sprite loaded into the Shader would have its upper left Corner centered on the Point where the actual Character's Feet touch the Ground (and the loaded Sprite would be displayed to the bottom right of the actual Character). So, the Sprite loaded into the Shader should have its upper left Corner placed where the actual Character's Sprite has its upper left Corner placed on Screen.

    The Problem is similar to scaling or rotating an Object via Lua, in which Case the Sprite's "RotationCenter" can and should be set as well, but since "graphics.shaderUniform" wants me to load in a Sprite via Filepath, I seemingly cannot just make it load a specificly defined Sprite that has been definied to have its RotationCenter at a specific Point (if that would even help me here).

    So, I am afraid the Character.Position is not what I am looking for in this particular Case, though I am currently using that (and Character.Size) to place and scale the Sprite in the Shader, but that needs anadditional Correction, and depending on my Character's current Animation, that Approximation (which necessarily includes Values which make no Sense to me) can sometimes fit perfectly and sometimes have a slight but noticeable Offset, possibly (but I can only speculate) because the Character's current Sprite might be cut down to only its visible Pixels (which would be the Difference between "graphics.getAnimationSize" and "graphics.getAnimationInnerSize", which I might be able to use for Scaling (not sure, I tried, it did not work, but maybe I just coded it wrongly), but that still would not help me with the Sprite's Positioning, I think).

    Forum Fan

    145 Posts

  • #4, by afrlmeTuesday, 26. March 2019, 11:51 5 years ago
    Sounds pretty complicated - especially if you used different canvas sizes for each of your characters animations.

    Imperator

    7278 Posts

  • #5, by caligarimarteTuesday, 26. March 2019, 14:36 5 years ago
    Sounds pretty complicated - especially if you used different canvas sizes for each of your characters animations.

    It's not that I myself was using different Canvas-Sizes, except for some very special Animations occasionally -- what I meant was that Visionaire tends to technically crop an Object's or Character's Sprite down to only the visible Pixels (the "graphics.getAnimationInnerSize()"-Rectangle), and that this might possibly be the unintended Change of Canvas-Size that might be changing the Offset -- but then again, that Suspicion of mine could be wrong. But the Character's Animation-Center is usually the same, so I don't know what else might might be the Reason for the Inaccuracy.

    Forum Fan

    145 Posts

  • #6, by MachtnixTuesday, 26. March 2019, 15:31 5 years ago
    What if you use the same sprite size for every frame and put one pixel onto the upper left side at equal position... so Visionaire believes that the frame size is the same..? Make the pixel 99% transparent... THAT is quick and dirty, isn't it? smile smile

    Sorry, your explanation doesn't help. It's to complicated. Because I never have used shaders I can't help any more.

    Thread Captain

    1097 Posts

  • #7, by caligarimarteTuesday, 26. March 2019, 16:07 5 years ago
    Hm, I just tried the "quick and dirty" Solution, and without changing anything about my current Code correcting (and now hyper-correcting) the Values, the Result does indeed look different again (fourth Part of the Image, the first three Parts have no Corner-Pixels and show the Problem so far -- you can see a flatly lit Duplicate of her behind her, for Testing-Purposes, with an Offset). So that at least shows me that it really is an Issue with Visionaire cropping the Sprite down to the visible Pixels, or else the Version with Corner-Pixels (which here are clearly visible for Testing, to see the Difference) would be placed and scaled the same as the others.

    I could, of Course, use the quick and dirty Solution and apply such Corner-Pixels to all Animations, but it would be dirty for a Reason, I suppose, because the Cropping is done to cut down Texture-Sizes, if I get that correctly -- so that would probably hurt the Performance overall, albeit just a tiny, tiny Bit. (It might also still prove problematic if any special Animation with different Image-Sizes and different Animation-Centers are being used, which can occasionally happen.)

    Other than that, it would certainly we a somewhat functional Workaround, I suppose, so thanks for the Suggestion, but I still wish I knew of a Way to get the Character-Sprite's Position on Screen, so I could prevent the technical "dirty"-Part of such a Workaround and also make it work with all Animations automatically.

    (By the Way, I can indeed get the cropped AnimationSize via "graphics.getAnimationInnerSize()", though it strangely gives odd Results inside the Shader... though I might get that to work. Now I'd only need the same for the Sprite-Position, be it cropped or uncropped.)

    EDIT: So, is there a Way to get that Position via "ActiveAnimations" and "CurrentSpritePos" maybe, for the Character? I have tried that, but maybe I just didn't code it correctly.

    Forum Fan

    145 Posts

  • #8, by MachtnixTuesday, 26. March 2019, 17:24 5 years ago
    Ich bin raus... Ich verstehe nur Bahnhof. und wenn ich den Übersetzer nutze (wie jetzt), kommt nur Müll raus.
    Warum die Figur doppelt erscheint, kann ich mir nicht erklären. Ist wohl wirklich ein Shader-Problem. Ich hatte es anfangs auch völlig falsch interpretiert, dank der Bilder sehe ich erst, was los ist.

    I'm out ... I understand only station. And if I use the translator (like now), only garbage comes out. smile smile
    Why the character appears double, I can not explain. Is really a shader problem. At the beginning I interpreted it completely wrong, thanks to the pictures I see what's going on.

    Thread Captain

    1097 Posts

  • #9, by caligarimarteTuesday, 26. March 2019, 17:39 5 years ago
    Ich bin raus... Ich verstehe nur Bahnhof. und wenn ich den Übersetzer nutze (wie jetzt), kommt nur Müll raus.
    Warum die Figur doppelt erscheint, kann ich mir nicht erklären. Ist wohl wirklich ein Shader-Problem. Ich hatte es anfangs auch völlig falsch interpretiert, dank der Bilder sehe ich erst, was los ist.

    Dass die Figur doppelt erscheint, soll ja so sein, genau darum geht es in dem Shader, dass ihre Grafik dupliziert und an dieselbe Stelle gesetzt werde, damit eine "Maske" für den Shader erstellt werden kann, womit ich dann andere Spielereien machen kann. Dass bei dem Beispielbild nur ein schlichtes Duplikat abgebildet ist, ist nur zu'r Veranschaulichung des Größen- und Positionierungs-Unterschiedes, aber das ist wohl in meinen ausschweifenden Textwänden gelinde untergegangen, sorry.

    The Duplicate is intentional, to be used as a "Mask" for the Shader -- that it is here visualized as a mere Duplicate is just to visualize the Difference in Size and Position.

    Forum Fan

    145 Posts

  • #10, by sebastianTuesday, 26. March 2019, 17:44 5 years ago
    getAnimationSize() should work.
    At least as i tried it last time, i got the sprite size incl. its zoom calculated in. the problem is that the sprite position is mostly not exactly the point where the sprite ends or exactly centeres. 
    So you need to consider that you need to substract also the spriteSize-"distance to position.x/y"

    Thread Captain

    2346 Posts

  • #11, by MachtnixTuesday, 26. March 2019, 18:02 5 years ago
    Ok, I get nearer... wink Slooooowly...
    Every character has usually a mask to seperate it from enviroment (alpha-channel). This one is always exactly on position. Can't you use them? You want - for example - that the 2D-character sheds a shadow?

    Thread Captain

    1097 Posts