Hello Guest

Author Topic: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform  (Read 7976 times)

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Hey,

I'm trying to implement a throwing knife with rope attached to it. It looks like this:



The left rope sprite is a tk2d tiled sprite, and the knife on the right is an object the character is able to throw in different directions. The anchor of the rope stays wherever the character is, but the middle handle of the other side of the rope needs to keep itself exactly where the knife's handle is at. This translates to modifying the tk2d tiled sprite dimensions.x value which is in pixel units, whereas the movement of the knife will be in Unity units (whatever it is called!)

What might I be able to do to keep these 2 points together? I feel like there is some kind of formula I can use.

Not sure if it's important, but I am using 100 pixels per meter on my tk2dcamera.

Or is there a complete other way I can implement this that is much easier?

Thanks

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #1 on: February 19, 2014, 01:29:25 pm »
I think the easiest way is to make 2 sprites: one for the knife and one for the rope. 

Then let the rope sprite increase in the x-pos the further the knife gets from the character. Mean while start tiling the rope sprite along the x-pos.

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #2 on: February 19, 2014, 01:42:38 pm »
It is already two sprites of course, the knife is its own sprite while the rope is a tiled sprite

What you've said is exactly the same as what I said except just reversed. Even if I keep the anchor of the rope on the knife I still need to extend the dimension.x value but in relation to the character instead of the knife.

DemiGoth

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 45
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #3 on: February 19, 2014, 05:08:46 pm »
It is already two sprites of course, the knife is its own sprite while the rope is a tiled sprite

What you've said is exactly the same as what I said except just reversed. Even if I keep the anchor of the rope on the knife I still need to extend the dimension.x value but in relation to the character instead of the knife.

Uhm, now I look at it again, you're right (was pretty tired earlier today  :-\ ).

Okay, algorithm then (not programming, just working it out in words leaving the programming to you :D )

Calculate distance between char.x and knife.x. In the middle drop the rope sprite and set rope.x length to knife.x-char.x  (there's a special distance function for it in Vector iirc - makes it easier when you can throw the knife over the y-axis as well).

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #4 on: February 19, 2014, 07:11:33 pm »
dimensions is in texel units. Assuming the anchor of the tiled sprite is the middle left (important, so adding width will simply extend it towards the right), you need to calculate the dimensions.y value that is required, which should be world_distance_between_sprites / tiledSprite.CurrentSprite.texelSize.y.

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #5 on: February 20, 2014, 09:58:44 am »
The anchor of the tiled sprite is indeed middle left.

you need to calculate the dimensions.y value that is required

Do you mean dimensions.x here? I don't see the y needs to be calculated as the y value is just the height of the sprite which in this case is 54 pixels and will stay constant. Only the length (dimensions.x) changes as it gets longer

world_distance_between_sprites / tiledSprite.CurrentSprite.texelSize.y.

'world_distance_between_sprites' Is this simply the distance between the knife and the anchor point of the tiled sprite?
And again, why is it texelSize.y?
« Last Edit: February 20, 2014, 10:44:42 am by arvz »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #6 on: February 20, 2014, 11:15:19 am »
I was assuming it was facing up. Feel free to swap with x, there should be no difference.

The world distance between sprites = (a.transform.position - b.transform.position).magnitude

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #7 on: February 20, 2014, 11:32:55 am »
Ok, I tried the formula you suggested:


            float newDimensionX = (transform.position - knife.transform.position).magnitude / ropeSprite.CurrentSprite.texelSize.x;
            Vector2 newDimensions = new Vector2(newDimensionX, ropeSprite.dimensions.y);
            ropeSprite.dimensions = newDimensions;

however it ends up looking like this: (The rope is not long enough. The pink line is a linerenderer I created for debugging purposes)




What do I put as the initial size for dimension.x? The sprite itself is 58x54 so I set to x=58 y=54. Does this even matter? (Trying different values doesn't seem to make a difference)
« Last Edit: February 20, 2014, 11:42:10 am by arvz »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #8 on: February 20, 2014, 12:10:57 pm »
Are you using local scale or scaling in any other way?

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: tk2d tiled sprites: Incrementing dimensions.x based on a moving transform
« Reply #9 on: February 20, 2014, 12:19:41 pm »
Actually, yes, I have set the x scale to 0.2. Have set this back to 1 now and seems to be working, thanks!  :)
« Last Edit: February 20, 2014, 12:27:16 pm by arvz »