How to detect if touch is in front or behind actor?
Tiny_Ideas
Member Posts: 326
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
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
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
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).
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. ?
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.
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.?
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. 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 . . .
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.
Yes, that was exactly what I was going to say*.
:P
*[technically a lie]
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"
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.)
Ah! I see, yes, makes sense now. The more you can whittle rules down to their most simple forms the better.
Good stuff. >-
@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.
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?