Results 1 to 9 of 9

Thread: Alignment between two images.

  1. #1

    Alignment between two images.

    I suppose this isn't necessarily UnDelphiX specific, but it's not even programming specific as it involves pure math basically but yeah lol.

    Anyway, basically I have a player sprite and then I have his attacking sprite, there seperated for collisions and such.

    Basically when he needs to attack, I create the attack sprite, animate, then kill it.

    But I'm having a bit of trouble aligning the attack sprite with the player sprite properly.

    There are 4 attack animations, depending on which way the player sprite is facing (which I keep track of through input movement), Up, Down, Left and Right.

    Depending on which facing, most of the trouble comes from centering the attack sprite to the player sprite.

    For example, if a user has the player facing Left and hits the spacebar, it will start the attack sprite attached to the left side of the player sprite.

    In which case the attack sprite should be positioned, as perfectly center, concerning the player's Top (Y) co-ordinate.

    For now, I use something like this (Handles UP FACING position):

    [pascal]X := TPlayer(Player).X - (TPlayer(Player).Width Div 2) + 3[/pascal]

    But even then it's not really dead on.

    And as unfortunate as I think it is, this will have to change for each direction, as sometime's it's the Y (top) position that requires centering.

    I don't have much of a problem getting the attack sprite on the edge of the player sprite though, Iv'e done that sort of thing with other stuff besides games before.

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Alignment between two images.

    A great way to solve this is with Constant arrays.

    Basically you make a constant aray that contains the offsets that you want for each direction - or rather delta offsets. Then based on the players facing direction you add the deltas to get the attack animation details.

    Heres code out of my head - might be slightly syntax incorrect:

    [pascal]
    Const
    AttackDeltas : Array[0..3] of Record DX, DY : Integer; End =
    (
    (DX:+10Y:-32), // Up
    (DX:-20Y:+10), // Left
    (DX:+10Y:+32), // Down
    (DX:+20Y:+10)); // Right

    // Inside procedure creating attack
    AttackAnim := TAttackAnim.Create;
    AttackAnim.X := Player.X + AttackDeltas[Player.Facing].DX;
    AttackAnim.Y := Player.Y + AttackDeltas[Player.Facing].DY;

    [/pascal]

    Hope this ansers your question
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Alignment between two images.

    Hmmm, I figured then would be a formula to it.

    Iv'e done it text before which is a little different, but based on small formula i had it center on the screen both X and Y wise.

    Can this be done for this instance? It would just make it easier for me for now, I'll keep that in mind though.

  4. #4
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Alignment between two images.

    Each scenario would require a different formula. It would depend on where the center of the images is, how large an offset you need etc.

    Also if you think about it - a formula needs CPU time to calculate - the use of an array like this is just straight memory access and there fore faster (Of course this only applies if you are really worried about every possible Frame you can get )
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  5. #5

    Alignment between two images.

    Well for the most part, although it may take longer, regardless of performance I tend to stick with what I already know about a given situation (assuming that I have done it similar before), once that's in place, I worry about performance or other factors if need be.

    If I ended up finishing the whole game, even if performance is fine, I tend to go dive back in and squeeze everything out of it, just for the hell of it lol.

  6. #6

    Alignment between two images.

    Just so you all know, I found a solution and even fixed up another sprite I have been drawing to be a bit better aligned, I use a formula in the form of:

    (For ABOVE or BELOW):

    X_1 := X_2 + ((W_2 - W_1) / 4);

    In some cases I use / 2.

    But basically for my purpose it's working, whether it's absolute for any situation or not lol.

    (For LEFT or RIGHT):

    Y_1 := Y_2 + ((H_2 - H_1) / 4);

    KEY:
    X = TOP
    Y = LEFT
    W = Width
    H = Height
    / = Divide By
    1 = The sprite to be positioned.
    2 = The sprite that is to have the other positions around it.

    And it seems to be working quite well ^_^.

    Took a little bit of brainstorming and messing around in paint in different situations to make sure it would work well enough for this particular case but I got it.

  7. #7

    Alignment between two images.

    X = TOP?

    You mean you've got X for the up-down axis?

    The convention is that X is the left-right axis... X is across ('a cross' get it? :lol: )

    It's your choice I guess, but it'll be confusing for other people reading the code.

  8. #8

    Alignment between two images.

    LOL!

    Well I dunno who else would be reading the code, you guys are pretty much the only people I know that could even begin to understand what's going on under the hood so to speak.....

    P.S. I know well that X is LEFT and Y is TOP, I have no idea how I managed to mix them up in the post, but I have been explaining all this how things are drawn to the screen relative of this, and so and so forth, so Iv'e gone through it a lot and must have muddled up .

  9. #9

    Alignment between two images.

    Simply,this is a artwork problem.
    Make sure your all image (animatiom frames) to the right position.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •