how to display text with an offset (x and y) AWAY from a static point

Hi all,

I'm spawning an actor with a display text behavior. This actor is being spawned when there's a collision between other actors in the scene. This collision can happen in many different places.

I want the display text to have an offset in the X and Y so that the text is show at the point of collision AND THEN 20 pixels more away from the center of the screen.

Basically: if the collision happens to the left of center, the text should display offset 20 px left; if the collisions happens to the top right of center, the text should display offset 20 px top right; etc.

How would I set the X and Y offsets to do this?\

Thanks in advance!

Comments

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited June 2017

    @Adrenaline

    I am assuming that one of the colliding actors spawns the new display actor at the location of the collision?

    Then in the display actor, create two attributes, XOffset and YOffset.
    Then test the display actor's position and set the offsets.

    Rule if self.position.X < device.screen.size.width/2
              XOffset = -20
    Otherwise
              XOffset = 20
    End rule
    

    and similar for YOffset

    Then put these attributes in the display text behavior as the X and Y position, and keeping it relative to actor.

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

    @Hopscotch said:
    Rule if self.position.X < device.screen.size.width/2
    XOffset = -20
    Otherwise
    XOffset = 20
    End rule

    and similar for YOffset

    This would only give you 4 positions (the 4 corners of a square) from what I can tell ?
    And each of these positions would be 28.28(...)pixels away from the centre (20*√2).

    I think you would have to use trigonometry to return a 20 pixel distance at all angles of collision . . . X=20*cos(v2A(centreX-selfX,centreY-selfY))+centreX . . . (etc etc) ?

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited June 2017

    Quite right @Socks, but that is how I interpreted the question. That the label is always a fixed 20x20 pixels from the collision point, just opposite from the center of the screen.

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

    @Hopscotch said:
    That the label is always a fixed 20x20 pixels from the collision point . . .

    Ah! Yes I see now, 20 pixels left and 20 pixels up (for example), which would be ~28.

  • AdrenalineAdrenaline Member Posts: 523

    @Hopscotch said:
    @Adrenaline

    I am assuming that one of the colliding actors spawns the new display actor at the location of the collision?

    Then in the display actor, create two attributes, XOffset and YOffset.
    Then test the display actor's position and set the offsets.

    Rule if self.position.X < device.screen.size.width/2
              XOffset = -20
    Otherwise
              XOffset = 20
    End rule
    

    and similar for YOffset

    Then put these attributes in the display text behavior as the X and Y position, and keeping it relative to actor.

    When you say it that way, it sounds so simple haha. I immediately started thinking like @Socks with calculating angles and such, but I wasn't doing that right either. D'oh! I knew if I posted here, I'd have an answer in the morning AND I'd feel stupid a the same time.

    Thanks guys, for providing on both fronts!

  • AdrenalineAdrenaline Member Posts: 523

    Okay guys, @Hopscotch your solution worked well enough, but @Socks hinted at something that I think would really be the brass ring.

    In the interest of complete honesty, I've changed my approach to this problem so I no longer need the ideal solution, but I figured I should follow up anyway (for future readers of this topic, maybe).

    @Hopscotch , your solution placed the text in the correct general direction, but not at the exact angle from the center point. If you see the image below, I was seeking a placement radius from the center point.

    @Socks , you started to get into angle calculations. Is that right?

  • HopscotchHopscotch Member, PRO Posts: 2,782
    edited June 2017

    @Adrenaline said:
    Okay guys, @Hopscotch your solution worked well enough, but @Socks hinted at something that I think would really be the brass ring.

    Damn! :D

    Yes, Socks gave you a solution for a relative angle between collision and center.

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2017

    @Adrenaline said:
    @Socks , you started to get into angle calculations. Is that right?

    X=Radius*cos(vectorToAngle(centreX-selfX,centreY-selfY))+centreX
    Y=Radius*sin(vectorToAngle(centreX-selfX,centreY-selfY))+centreY

    Relative to scene.

  • AdrenalineAdrenaline Member Posts: 523

    @Socks said:

    @Adrenaline said:
    @Socks , you started to get into angle calculations. Is that right?

    X=Radius*cos(vectorToAngle(centreX-selfX,centreY-selfY))+centreX
    Y=Radius*sin(vectorToAngle(centreX-selfX,centreY-selfY))+centreY

    Relative to scene.

    I should probably gain a better understanding of sin and cos since most of @Socks answers for me involve them so frequently...

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2017

    @Adrenaline said:
    I should probably gain a better understanding of sin and cos since most of @Socks answers for me involve them so frequently...

    There's almost nothing to understand about sine and cosine, you can learn its practical applications for something like GameSalad in a few minutes.

    Draw a line from the centre of a circle until it hits the edge (the red line in this image).

    If you were to draw a dot at the point where the line hits the edge of the circle and measure from the centre of the circle to the dot - horizontally - that would be the cosine of the angle of the line, the distance from the centre of the circle to the dot - vertically - would be the sine of the angle of the line you've just drawn.

    Hope than makes sense !

    It's a pretty simply concept, but you'd be surprised by what you can do with it.

  • AdrenalineAdrenaline Member Posts: 523

    Thank you @Socks for taking the time, the explanation is certainly appreciated.

    I actually did some Youtubing right after posting that I SHOULD learn more about sine and cosine, and I DID learn more about sine and cosine. You're right, it's simple :smile:

    Thanks again

Sign In or Register to comment.