Formula for relating postion and velocity direction
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.
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
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
So question 2 would be more fitting.
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.)
@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.)
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.
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...
@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.
this is the velocity attribute i have for actor2.
spawnX and spawnY are the 2 variables that are constrained to actor1's position.
Is the formula the same for no matter where actor1 is on the scene?
Edit: yes it is
Thanks everyone for helping
((vectorToAngle(240-game.spawnX,160-game.spawnY ))+360)%360
Glad its working for you!
RThurman
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
RThurman