Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Chat / How to set position of spirte relative to the position and rotation of another sprite?

Author
Message
Zigi
9
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 17th Jul 2018 11:34 Edited at: 17th Jul 2018 11:39
Hi all!

So I have two sprites, a big yellow and a small black on top of the yellow sprite like so:


What I would like to do is keep the black sprite in that position relative to the yellow sprite even if the yellow sprite rotate and move. It is usually called the parent and child where the black box is the child of the yellow
It is a pretty common topic and I know I need to use trigonometry Cos and Sin and I did search the net for a formula to do this but unfortunately none of the formulas I've found works. The black box does not stay in position as I want it to.

this is the two formulas I've been trying, these are not AppGameKit specific but formulas that used for this in general and I'm trying to implement them:
childX = Cos(parentAngle) * parentWidth + parentX
childY = Sin(parentAngle) * parentWidth + parentY

childX = (Cos(parentAngle) * childX) - (Sin(parentAngle) * childY) + parentX;
childY = (Sin(parentAngle) * childY) + (Cos(parentAngle) * childY) + parentY;

I have no idea what am I doing, only did find these formals on the net and it seem to worked for others but it does not work for me.
Could anyone help me with the math please?
I would really appreciate it.

Thanks.

Attachments

Login to view attachments
Bengismo
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Jul 2018 11:56 Edited at: 17th Jul 2018 16:13
I use this function....



You have to call this whenever the parent sprite moves or rotates

Ive a few others that do similar things like relative placement or turning to face other sprites etc..
Phaelax
DBPro Master
15
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Jul 2018 14:14
Just change the offset of the child sprite, no trig needed.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Bengismo
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Jul 2018 14:32
Nice!

That last line should be setSpriteAngle(child, getSpriteAngle(parent)) no need to add 3 degrees again or its always offset from its parent by 3.

baxslash
Bronze Codemaster
11
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th Jul 2018 15:13
What about using GetWorldXFromSprite( iSpriteIndex, x, y ) and GetWorldYFromSprite( iSpriteIndex, x, y )
No need to mess with sin/cos, just set the position using these two functions and then set the angle...
Using AppGameKit V2 Tier 1
Bengismo
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Jul 2018 15:23
^^ Thats genius

Both these produce exactly the same result
xn#= x*cos(angle#) - y*sin(angle#) +GetSpriteXByOffset(parent)
yn#= y*cos(angle#) + x*sin(angle#) +GetSpriteYByOffset(parent)

xn#= GetWorldXFromSprite( parent, x, y )
yn#= GetWorldYFromSprite( parent, x, y )

So its just an easier way of getting to the same result
Phaelax
DBPro Master
15
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Jul 2018 17:58
Quote: "no need to add 3 degrees again or its always offset from its parent by 3."

woops, yea my mistake.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Zigi
9
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 18th Jul 2018 15:40
Thanks a lot for the replies guys.
Unfortunately though it still doesn't work exactly as I want it to

@Bengismo
Your solution works for positions but in case I start rotating the yellow box, the child box is shifting all over the place.

@Phaelax
You solution works great for both positioning and rotation in the example you provided but in case I want to change the position of the child box and say I wan it to be more in the middle of the parent on the X and do X+20, I have the same problem as with @Bengismo solution, the box is shifting away doesn't keep it relative position to the parent when rotating. Also, what is the purpos of the variable a#? if I replace it with a 0 it seem to work the same.

Unfortunately I don't know and I don't understand the math behind this All the math I find online may work in a certain case like now but it does not for different scenarios. There are engines out there though with a built in function to do just this set a relative position and angle to a parent and it works for all scenarios doesn't matter where the child and parent is positioned and what is the initial rotation, it works. How something like that could be accomplished? A universal formula that just works for all scenarios?

Thanks for all the help again.
Bengismo
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 18th Jul 2018 15:47 Edited at: 18th Jul 2018 16:01
Do you have any code?? Id guess your not using the function correctly

The function I posted works fine no matter where the parent sprite is or how its rotated....


Maybe try this....
Phaelax
DBPro Master
15
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Jul 2018 17:20 Edited at: 18th Jul 2018 17:49
Quote: "You solution works great for both positioning and rotation in the example you provided but in case I want to change the position of the child box and say I wan it to be more in the middle of the parent on the X and do X+20, I have the same problem as with @Bengismo solution, the box is shifting away doesn't keep it relative position to the parent when rotating. Also, what is the purpos of the variable a#? if I replace it with a 0 it seem to work the same. "

a# is just used to move the big square in a circular motion around the screen, it's for demo purposes only.

Ok, so the default pivot/offset point for a sprite is its center. My parent box is 200 by 200 so the center point is 100,100. With the child square positioned at the same spot as the parent, the child offset should be 100,100. This will keep the child in the upper left corner of the parent. To move from that relative spot you need to change it's offset as well. The closer to the center of the parent the child gets, it's offset will get less and less. So any time you change the child's relative position to the parent, you'll need to adjust the child's offset as well.

The child in this example is 50x50, so if you want it positioned at the center of the parent:

getSpriteX(parent) + 75
getSpriteY(parent) + 75

With the top left corner of the parent being 0,0, move the child over by 75,75 to place the center of the child at 100,100 (which the center of the parent). Subtract 75 from 100 to determine how far the child is from the offset of the parent and that's how far you offset the child.

I drew an example to help illustrate. I used different size dimensions of the boxes. The parent is now 200 by 150 and the child is 50x50 and positioned [38,40] from the top left corner of the parent.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Attachments

Login to view attachments
Zigi
9
Years of Service
User Offline
Joined: 5th Jul 2009
Location:
Posted: 19th Jul 2018 10:51
Thanks guys I managed to get it working with both solution and set the child to be anywhere on the screen as I wanted
Thanks a lot!

Login to post a reply

Server time is: 2018-08-14 10:16:45
Your offset time is: 2018-08-14 10:16:45