I need tiny bit of math to make XY of one actor relative to rotation of another.

Xazo-Tak

  • Posts: 28
Can somebody make a piece of code to make one actor change its X and Y to always keep its position relative to the parent actor I choose always the same? In other words, like making something always stay 50 pixels to the left (Parent actors left, not real left) and 25 pixels behind (Parent actors behind)?

Because I want a game with customizable ships, but I don't know how to get an actor to always stay a certain amount to the left and front of another, and be facing in the same direction.

If you have any questions, I will check this topic often, I don't do request and runs waiting for a message.

Oh about what's in it for you, I will make an advert on the menu to automatically link to your games when clicked, and if you play on kongregate if I upload it there (logged in) some special parts and extra score.

« Last Edit: September 22, 2011, 04:02:35 pm by Xazo-Tak »

Xazo-Tak

  • Posts: 28
It's been days, and 0 replies 12 views?
GRRRRRRRRRRAAAAAAAAAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRRR

Hectate

  • *
  • Posts: 4643
I think that the issue you're having is you've presented a potentially difficult problem with minimal perceived reward. That said, I know that I don't have time for any of my own projects, much less committing myself to work on another person's. Perhaps other people are in the same boat as me?

Additionally, I'm trying to imagine what you're requesting and I really don't think I get it. Could you describe what you're trying to accomplish?
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Xazo-Tak

  • Posts: 28
I think that the issue you're having is you've presented a potentially difficult problem with minimal perceived reward. That said, I know that I don't have time for any of my own projects, much less committing myself to work on another person's. Perhaps other people are in the same boat as me?

Additionally, I'm trying to imagine what you're requesting and I really don't think I get it. Could you describe what you're trying to accomplish?
What would be satisfying? And I just made an edit, hopefully it should make sense.

Hectate

  • *
  • Posts: 4643
Well, to fix something to a specific position relative to another actor is fairly straightforward. You pick one actor to be the "parent", and you do all your movements to that actor (up, down, left, right). The other actors are "children" and once you determine where you want them, just continually set their X,Y coordinates based on the parent's X,Y coordinates, plus the necessary offset (which might be negative though to subtract).

The issue you'd encounter with this is if you wish to add rotation to it. Since rotating the parent actor doesn't actually change it's X,Y coordinates, you'd have to use math to determine the adjusted coordinates of any children as well as apply the same angle to them.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Xazo-Tak

  • Posts: 28
Well, to fix something to a specific position relative to another actor is fairly straightforward. You pick one actor to be the "parent", and you do all your movements to that actor (up, down, left, right). The other actors are "children" and once you determine where you want them, just continually set their X,Y coordinates based on the parent's X,Y coordinates, plus the necessary offset (which might be negative though to subtract).

The issue you'd encounter with this is if you wish to add rotation to it. Since rotating the parent actor doesn't actually change it's X,Y coordinates, you'd have to use math to determine the adjusted coordinates of any children as well as apply the same angle to them.

Precisely my problem! I do not know what bit of math I need. Just that one very small bit of code could make my day. Offset and relative x y is easy.

Hectate

  • *
  • Posts: 4643
Well, I never took trigonometry so I can't answer. I can kinda make sense of what is described when I Google various examples and tutorials, but it's not something I can easily implement myself. Someone else will have to help...
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Shakura

  • Posts: 26
Definitely learn trigonometry if you don't know it for this kind of coding... Thought about something similar myself a while ago. Can't you just place the actors in the scene then give them a stick joint so that when one turns the other will stay at relative position?

Anyway, I'll puzzle this out in a moment, general code should be useful for anyone requiring it... for now, I'm trying to decide whether to just go straight to angles and trig or whether vectors would work, 'cept I forgot a lot of vector math >.>

Here's the basics: Figure out the angle between your main actor and your following actor (draw your main actor on a piece of paper, then draw the other actor... now draw a line straight out horizontally from the main actor until its above the lower one, then draw straight down, and then connect the start and end lines so you get a triangle...)... Anyway, for 50 left 25 down, your angle is ATAN(-25/-50), which means Arc Tangent of 25/50 (getting the angle in the triangle from the short sides)... Anyway, there's a block in trig that says "sin []", use that and switch sin to atan, then put in your -25/-50, and set that to an angle attribute... do that just when created. There's some problems with using straight up trig and corrections I won't get into now, but it'll be later on in the post...


Aaaanyway, you now have an angle that your follower object should be at relative to your main object... Next we get the hypotenuse of the triangle, which tells us the overall distance between the objects. Just use the square root math, and do: sqrt[[50*50]+[25*25]] as your hypotenuse



Here's the code in code blocks, not actual code... Hope its clear enough for now

Creation:
Angle1=as degrees [[Pi]+[ATan[-25/-50]]
Hypotenuse=sqrt[[50*50]+[25*25]]

Always:
Set X of Self to [x of MainObject] + [Hypotenuse]x[sin[As Radians[[Angle1+DirectionOfMainObject]]]
Set Y of Self to [y of MainObject] + [Hypotenuse]x[cos[As Radians[Angle1+DirectionOfMainObject]]]

And I think that should work. Going to go test it now...


Aaaanyway, tested and it works. Will post screenie of some code shortly.

Last edit: Here's my sample code. Player is the object its following, Spike is an actor attribute, Angle and Hypotenuse are number attributes. As Radians is in the Trig subsection of Math, and the block normally reads "as degrees".



The math part took all of 5~10 minutes to figure out, which is probably b/c I haven't touched trig in 6 months... Really, stuff like this is easy if you get the relevant education; I suggest physics if you really want to know how to do relative motions and other things, since it does a lot more visual and applicable stuff and you get a lot of math that you need for it anyway.

« Last Edit: September 22, 2011, 08:14:45 pm by Shakura »

Xazo-Tak

  • Posts: 28
Woah... I didn't think the code for that was that complicated... I should definitely learn trigonometry. But anyway, think you could upload that to stencylforge? And I'll set the kongregate API to recognise you since I'll probably put the game there.

Xazo-Tak

  • Posts: 28
Well this is just GREAT. I can't do anything till they upload that code, and they haven't had any activity since that message.

Alexin

  • *
  • Posts: 3132
Why don't you replicate the behavior from the screenshot? You don't need trigonometry to do it.
"Find the fun"
alexin@stencyl.com

Xazo-Tak

  • Posts: 28
Why don't you replicate the behavior from the screenshot? You don't need trigonometry to do it.

I thought that wouldn't work. Turns out it does, but I seem to have the position of the turret will be closer/further to the ship, depending on my rotation. Either it's the original code, or more likely a mistranscribing.

Shakura

  • Posts: 26
Sorry, thought that code was good enough, I'll review now...

What's the problem you're having? I'll try to get generic code up on setncylworks soon.

Look for Remain Relative to Object on Stencylforge.

Edit: Found a simpler way of doing this. As for your problem, I'm guessing it's circling around the top-center of your ship; if you want to use that code, just add an X offset and Y offset to the [X of MainObject] and [Y of MainObject] blocks. The coordinate's Stencylworks is using for your ship is probably the top left corner of the sprite, not the center. So if the sprite for the ship is 32x32, replace [X of MainObject] with [  [X of Main Object] + [16]  ] and the same thing for the Y to get it to rotate about the center.


As for a simpler method, here you go:
[set x of [self] to ( {cos [direction of [ObjectToKeepRelativeTo]]}x distancetokeepfromobject)+ x of [[ObjectToKeepRelativeTo]]
[set y of [self] to ( {sin [direction of [ObjectToKeepRelativeTo]]}x distancetokeepfromobject)+ y of [ObjectToKeepRelativeTo]]

or for having an offset,
[set x of [self] to (( {cos [direction of [ObjectToKeepRelativeTo]]}x distancetokeepfromobject)+XOffset+ x of [ObjectToKeepRelativeTo]]
[set y of [self] to (( {sin [direction of [ObjectToKeepRelativeTo]]}x distancetokeepfromobject)+YOffset+[y of [ObjectToKeepRelativeTo]]

Uploading to forge now...

Modified again: Forget the stuff I typed above, look for the upload on stencylforge. Fixing stuff now that it's tested and uploading...
Look for the behavior "Remain Relative to Object" on Stencylforge. Trying to figure out how to make it non-private and some other stuff right now, buttons not appearing where forge says they should be, and it thinks I'm "unknown" as uploading despite it saying I'm logged in...

Edit again: Got it up, both without being logged in and with being logged in, problem was I wasn't logged in to Stencylworks itself and wasn't prompted to until I restarted the program... So if you're not logged in, there's a *bunch* of stuff under "my resources" on stencylworks that people probably didn't realize is free to use since they didn't realize, like me, that they weren't uploading proper... AAaaanyway behavior is up and public. Very simple this time (sorta)

« Last Edit: October 08, 2011, 12:31:37 pm by Shakura »