How to detect if touch is in front or behind actor?

Hello all,

I am wondering how to detect if a touch is in front or behind an actor that is moving and rotating accordingly in a circle, therefore the front of the actor would always be changing.
By front it is any space that is in line or a head of the actor.

- moving along the x axis was easy, simply if touch x is smaller or larger than position x.
Just would like to know how to do it when moving and rotating in a circle?

Thank you

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    My idea involves figuring out what quadrant of a circle the actor is in and then based on the vector between the location of that actor and the touch position determining relative location.

    It sounds tricky but I'll work on it and we'll see if someone else has a better way.

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

  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    I'm not sure how you are moving your actor around a circular path, but it most likely involves an angle - if it does then you could simply check if the angle of the touch is larger or smaller than the angle** of the actor.

    You can check the angle of the touch by using vector to angle (one end is the centre point of the actor's circle, the other end is the touch position).

    (**by 'angle' I mean its position in its circular path rather than its local [rotational] angle).
  • Tiny_IdeasTiny_Ideas Member Posts: 326
    @tatiang and @socks

    Thanks, not quite understanding,
    To help, the actor will rotate to face direction it's moving. So yes the angle will be changing.
    So I am guessing I am now trying to determine if touch angle is actors angle plus or minus 90 degrees.
    I would need now to find the angle that the touch is from the actor. Is that by using vector to angle, actors position x, actors position y, touch x, touch y. ?
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    @Jeromy

    Here's a quick demo project, it should give you the basic idea, it momentarily screws up as it passes the 180° mark (9 o'clock on a clock face) as GameSalad jumps from 180 to -180 (rather than going from 0° to 360°) . . . but that should be easy enough to clean up.


    Link: https://www.mediafire.com/?ju3i96viawb4fm6

    Move your mouse around the circle, it will show you when you are in front or behind the white moving actor.
  • Tiny_IdeasTiny_Ideas Member Posts: 326
    @Socks

    Thanks for the demo, a nice demo.
    One question, the actor is going to be spawned, and there will be more than one actor.
    I haven't had the time to fully add to it yet, but is it possible to make it all run off each actor.
    Instead of that display text actor , each actor displays whether touch is in front or behind in regards to itself.?

    Also, I noticed the issue with the 180 to -180 where it goes from front than to back. How would one fix that, or make a workaround.?

  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    . . . is it possible to make it all run off each actor.
    Sure, just make the vector-to-angle calculation specific to each actor - so it is calculated from each actor's position to the touch position.

    Image the same question with your earlier example of an actor just moving along the X axis, in that scenario you'd just get each actor to display its position minus the touch position - its the same deal with the vector-to-angle calculation, just type in the values relative to each actor.
    Also, I noticed the issue with the 180 to -180 where it goes from front than to back. How would one fix that, or make a workaround.?
    I can't really think of anything off the top of my head, but it's basic maths, you want something to go from 0 to 360 - but it's going from 0 to 180 - then -180 to 0 - I'm sure you could beat those numbers into submission with a couple of basic calculations - maybe someone with more coffee in them than me can chime in . . .




  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited January 2014
    @Jeromy -- Another way to do this is to get the difference between the two angles. That is, determine the difference between the actor's rotation and the mouse's angle (from the center of the actor).

    Its a simple subtraction:
    self.rotation - (vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y )

    But.... the difference also needs to be 'normalized' to take into account the "180 to -180" stuff that @Socks mentioned. To do this you need to modify the expression to read as follows:
    (( self.Rotation -(vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y ))+180)%360)-180

    Here is an example.
  • SocksSocks London, UK.Member Posts: 12,822
    edited January 2014
    @RThurman

    Yes, that was exactly what I was going to say*.

    ;) :P





    *[technically a lie]
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    @Socks -- LOL it gets better! You can do it with only two behaviors!

    Rotate: Counter-clockwise
    Display Text:((((( self.Rotation -(vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y ))+180)%360)-180)<0)and"Front")or"Behind"
  • SocksSocks London, UK.Member Posts: 12,822
    Rotate: Counter-clockwise
    Display Text:((((( self.Rotation -(vectorToAngle( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y ))+180)%360)-180)<0)and"Front")or"Behind"</blockquote>

    Where does the Rotate come in ? How does the 'hero' actor travel in a circle ?
    @Socks -- LOL it gets better! You can do it with only two behaviors!
    I can do it in one . . .

    Constrain Answer to: RThurman


    : )
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    edited January 2014
    Where does the Rotate come in ? How does the 'hero' actor travel in a circle ?
    Its just a regular rotate behavior. Like in the example file. I was trying to show that you could replace both the constrain behavior and the rule with just one display text behavior.

    Attached is an example.

    As for the hero actor traveling in a circle. Well.... I forgot that part. It just rotates. (But the principle of finding the difference between the two angles is the same.)
  • SocksSocks London, UK.Member Posts: 12,822
    @RThurman

    Ah! I see, yes, makes sense now. The more you can whittle rules down to their most simple forms the better.

    Good stuff. :)>-
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880
    @Socks -- Its all fun! Pretty amazing how flexible and powerful GameSalad really is. And it's still only in Beta! :D

    @Jeromy -- there are other ways to check for front/back. For example, you could also check for the touch position in local coordinates (as opposed to the global coordinates that are usually used). You can find the touch position in terms of the actor's local coordinates with some "sin/cos" action. Attached is an example.
  • Tiny_IdeasTiny_Ideas Member Posts: 326
    @Socks and @RThurman

    Thanks heaps guys. I managed to get it working with a few modifications.

    Is it possible to be able to still use the "detect front and back" using the edge of the actor, rather than the middle?

    For a rectangle 50x 80 , with the long width being the front and back, can I detect it from another 40 pixels in front and or behind. Even if its rotating.

    I first figured it was simple just add 40 pixels on the the equation, but that will only work if straight and for only the front or the behind. Not both. Any ideas?
Sign In or Register to comment.