Formula for relating postion and velocity direction

BluSpyderBluSpyder Member Posts: 129
edited February 2012 in Working with GS (Mac)
I've been trying to think of a formula that relates the position of an actor to the direction of a different actors velocity.

ie. Actor1's position xy is 480,320.... so Actor2's velocity direction is 215 (this is diagonal from the top right of the screen to bottom right)

But now my Actor1 is moving from 480,320 to 0,320 (or 0,0 // 480,0). So I need a formula for Actor2's direction so it is constantly matching with the position.

I already have Actor1's postions of x and y constraining to attributes positionX and positionY.
Also, I don't want to just change when Actor1 reaches a new position. Like (When positionX= 0 and positionY= 320 -> change velocity to 315) I want it to be constantly changing as actor 1 moves to the position.

To help, like stated earlier, the first angle for the top right of the screen is 215, and the top left i think should be 315. Any help would be awesome.

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited February 2012
    Not quite following the question. I get at least two possible questions out of this:
    1) How do you get actor2 to point at actor1?
    or
    2) How do you get actor2 to point in a certain direction as actor1 is moved up and down (or left and right)?

    For 1) you would use a rotate to position behavior

    For 2) you would make a formula that relates Actor2's angle (0 to 360) to Actor1's Y position (0 to 480).
    So that: Actor2.rotation = Actor1.Y*(360/480)
    Or perhaps it is: Actor2.rotation = Actor1.Y*(480/360)

    Are either one of these questions close to what you are asking?

    RThurman
  • BluSpyderBluSpyder Member Posts: 129
    edited February 2012
    I guess what you could say is, I want to have actor2 pointing away from actor 1 and towards the center. Like imagine a line between the very center and the moving Actor1 (which is moving along the edge of the scenes. That's the angle of direction i need.

    So question 2 would be more fitting.
  • BluSpyderBluSpyder Member Posts: 129
    Just tried your formula. I think this might actually not be allowed, as no changes were occuring as actor1's y was changing. I think the direction in velocity may stay the same when the game starts, unless a whole new velocity attribute is introduced. may need Tshirtbooth to confirm that, as he showed me that Timers cannot have a variables anymore.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited February 2012
    The formula for finding the angle from an actor to the center looks like this:
    vectorToAngle(240- self.Position.X ,160- self.Position.Y )

    That assumes that the center is at 240,160. (So you would need to change those numbers to whatever your center might be.)
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    Im not really following either. You said you want actor 2 to point in the opposite direction from actor1 but you also want it to point to the center. It seems like you are trying to over think what your going for or hold back all the information we need. Maybe try drawing a picture just to explain what it is you are wanting. I also dont see how an actors velocity comes into play with the direction the other actors are pointing.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited February 2012
    @tenrdrmr -- I agree, it is hard to interpret how the word 'velocity' is being used here.

    @Wingology -- perhaps Actor2 is an arrow that points at the center of the scene, but is constrained to the x,y of Actor1 ? In that case, use constrain attribute behavior in Actor2:

    Constrain Attribute: self.Rotation To: vectorToAngle(240- game.Actor1X ,160- game.Actor1Y )

    Once again, your center might be different. And 'Actor1X' is a game level attribute that holds the x position of Actor1. (Same for Actor1Y.)
  • BluSpyderBluSpyder Member Posts: 129
    edited February 2012
    Actor1 position = 480,320
    So velocity of actor2 = direction 215 at game.speed

    Say when Actor1 is moving to 0,320, at 240,320 the velocity of actor2 should be = 270 at game.speed
    So actor2 is always moving towards the center (but continues through it and wraps on the scene to appear back at the origin of actor 1) and away from Actor1.

    Here's a image i quickly made.

    image


    So as Actor1 moves across the screen, the direction of the spawning actor2's velocity needs to constantly change to always be towards the center as it spawns from actor1. Actor 2 wraps when it reaches the other edge of the scene, so it's not disappearing, so it needs to match the direction.

    Sorry if I'm still not making any sense lol...
  • Rob2Rob2 Member Posts: 2,402
    edited February 2012
    So actor2 should always spawn at a point lying on a theoretical line that joins actor1 to the centre ?
  • BluSpyderBluSpyder Member Posts: 129
    edited February 2012
    actor2's spawn isn't relevant, its just the angle between actor1 and the center. and then that angle needs to be constantly changing actor2's direction of velocity.


    @RThurman i think that might be what i'm looking for (vectorToAngle(240- self.Position.X ,160- self.Position.Y ))

    but when I input that into the formula field for actor2's velocity, the direction of actor2 doesn't change.

    image

    this is the velocity attribute i have for actor2.

    spawnX and spawnY are the 2 variables that are constrained to actor1's position.
  • BluSpyderBluSpyder Member Posts: 129
    edited February 2012
    Solved. For some reason the velocity direction wouldn't update ever when actor1 was moving. So i placed a timer around the velocity behavior for "every 0.1 seconds"

    Is the formula the same for no matter where actor1 is on the scene?
    Edit: yes it is

    Thanks everyone for helping
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    The formula is functional as you have it, but to get the exact numbers you are expecting the equation is:
    ((vectorToAngle(240-game.spawnX,160-game.spawnY ))+360)%360

    Glad its working for you!
    RThurman
  • BluSpyderBluSpyder Member Posts: 129
    edited February 2012
    Awesome thanks. What's the difference when adding the +360)%360 ?
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited February 2012
    The difference is that the previous formula gives you negative angles. However, the updated equation gives you the angles exactly like you would expect them. For example, in your drawing, you had an angle of 215. The previous formula gave an angle of -145, but when you add the '+360)%360' it will give the angle 215 as you expect.

    Angles can be negative or positive, and they can be greater than 360 or less than 0. For example, the angle 720 is still a valid angle. (And it will point to the same place as the angle 360.) Also the angle -145 is a valid angle. (And it points to the same place as 215.)

    RThurman
  • BluSpyderBluSpyder Member Posts: 129
    Oh ok, I understand. Thanks again RThurman.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    Glad to help.

    RThurman
Sign In or Register to comment.