How to change actor position relative to actor

ETGgamesETGgames Member, PRO Posts: 190

When you use change attribute position x and y, and want to change the position based on its current position, you would do change position x to self.position x - 5 or whatever and y to self.position y - 5. However, this is a change relative to the scene. If the actor is at an angle, how would i make it change position like this. I want it to change its position by the same distance each time relative to its angle to another actor. Maths geniuses, help? Thanks

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited October 2015

    Something like this? Right & left arrow keys to move 50 pixels, space bar to rotate (and reset position).

    I realize it's not relative to another actor but the way I initially set it up, I had another actor a certain distance away along a line (same slope) and calculated it based on the slope of the line. I resized that actor into an actual line but you could calculate the actual slope of the line containing both actors (their centers) and then use that in place of self.Rotation which is what I used.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SocksSocks London, UK.Member Posts: 12,822

    @tatiang said:
    Something like this?

    Don't let Ketchapp see that, they'll have it on the App Store before you can say 'vector to angle'.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited October 2015

    @Socks Vector to angle!

    By way of explanation, @ETGgames,

    If we imagine that one actor is at point x1y1 and one actor is at point x2y2 -- two vertices of a right triangle -- and that d is the distance between them (in my demo, I set d to 50), we can calculate the angle A using vectorToAngle (in my demo, I set the rotation of the actor to 20° instead of calculating the angle). Knowing d and A, we can use trig functions (see http://rml3.com/a20p/images/trig_sin_cos_tan.gif) to calculate the missing variable. For example, sin 20° = y/d. Using a calculator, sin 20° = .342. So the equation becomes .342=y/50. Solving for y, we get 17.1. To find x, we use cos 20° and solve for x in a similar fashion.

    So you’ll notice that in my demo, I have Change Attribute self.Position.Y to self.Position.Y ± self.distanceToMove * sin( self.Rotation ). I’m adding or subtracting d * sin(A) to the current Y position of the actor. And for x, the expression contains cosine.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Here's another version that calculates the angle between two actors and also allows you to hold down the arrow keys for continual movement. You can place the shaker anywhere in the scene before previewing.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SocksSocks London, UK.Member Posts: 12,822

    @tatiang said:
    Using a calculator, sin 20° = .913.

    Not sure where that figure comes from, at first I thought you'd mixed up cosine and sine as the cosine of 20deg must be somewhere in that area, but calculating it showed the cos of 20deg to be 0.939 - and obviously .9 can't be the sin of 20deg ?

    By the way for some unknown reason calculators often return mistaken results for simple trig functions.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    @Socks thanks for catching that. I was trusting Spotlight to be my "maths genius." I've fixed it in the original post.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • SocksSocks London, UK.Member Posts: 12,822
    edited October 2015

    @tatiang said:
    Socks thanks for catching that. I was trusting Spotlight to be my "maths genius." I've fixed it in the original post.

    Ah! I see, Spotlight defaults to measuring angles in radians rather than degrees, so the sine of 20 radians is 0.913, whereas the sine of 20deg is 0.342. I never use radians, they are the inches to degree's centimetres, ban them I say.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited October 2015

    I figured as much. I haven't figured out how to force Spotlight to use degrees. Including the degrees symbol doesn't seem to make a difference. Maybe Siri will know...

    Edit: sind(20)

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • ETGgamesETGgames Member, PRO Posts: 190

    Haha thanks socks, I don't really understand what you're saying, I don't think it solves my problem. However, after hours of hard thinking to what we are learning about component vectors in school right now, I came up with a solution. I found the vector to angle using linear velocity, since my player is moving around a circle with accelerate towards the centre of the circle and an initial change velocity right. Then I saved the angle as an angle attribute so I wouldn't get negative numbers for angles over 270. Then, since I knew I wanted my player to move 35 pixels inwards at any point on the circle, I did change pos x to pos x - 35cos(myAngle) and change pos y to pos y - 25 sin(myAngle) and it worked beauuutifuuly!!!!

Sign In or Register to comment.