It looks like you're new here. If you want to get involved, click one of these buttons!
Hello,
I'm very new to codea and at the moment I'm trying to understand how the touch verbs work.
For the most part I've been using currentTouch and I was wondering if there was another way of doing it.
I was wondering if someone could please explain to me how I could make the algorithm know if the user was to touch an ellipse for example.
Thanks, Ilan
Comments
@ilankess
Using CurrentTouch, you would use CurrentTouch.x and CurrentTouch.y to determine the x,y coordinates where the screen was touched. To determine if an ellipse was touched, you would have to calculate if the returned x,y value was within the bounds of the ellipse. That can be done, but beyond what I want to get into right now. An easier use would be to determine if a square or rectangle was touched. For example, if CurrentTouch.x was greater than the left value and less than the right value, and CurrentTouch.y was greater than the bottom value and less than the top value of the rectangle, then the rectangle was touched. Here is a small example. Touch the rectangle, then touch elsewhere.
@ilankess
Here is an example for the ellipse. Touch inside then outside the ellipse.
Thanks @dave1708
Really helpful
But why does this work, (y-400)^2/50^2+(x-200)^2/100^2 <=???
(why does it represent that areas coordinates?)
Thanks
@ilankess
That's derived from the formula for an ellipse (x/a)^2+(y/b)^2=1. Any x,y value that results in a value of 1 will be on the edge of the ellipse. Any x,y value less that 1 will be inside the ellipse. Any x,y value greater than 1 will be outside the ellipse.
Here is the previous program, but it allows you to change the size and position of the ellipse. Change the a and b values to create different size ellipses. Change the xc and yc values to move the center of the ellipse.
Pure brilliance. Thank you so much!
@ilankess
I don't like all the coordinate calculations: this will be very tedious specially if there are many objects you want to touch
I would rather use the physics API that manages the contact events. I wrote the following exemple for you. It seems long but it is mainly because i have put a lot of comments. Try it and let me know what you think.
@Jmv38
If all you're after is to detect if an object is touched, here is an example that displays 50 white balls. When you touch them, they'll turn red. The amount of code needed to check if 1 or 50 of the balls is touched is the same. There could be 500 balls and the only thing that needs to change is the count value.
@dave1707 you are right if all the objects are identical. But if they have different size and shapes, it can become complex. Wait, hummm.. on the other hand, it should be easy to design a class for each object that include a self check function. Deuce. I guess i simply don't like the idea to check all the objects in lua, cause i expect it to consume computing power and FPS. If the Objectve C of the physics API does the job, it should be faster?
By the way, there is a flaw in my program above: woks fine whens there are just touches, but if i press and slide my finger, then the touch or collide seems blocked until i touch another object (touches on the same object are not printed)...
@Jmv38, sorry for the late reply but looking at it now I am quite interested by your post.
I am very new to lua and have only written 2 small apps in codea with the knowledge i have.
If you wouldnt mind answering a couple of my questions that would be greatly appreciated... (sorry if i sound like an idiot)
1) what is physics.body?
2) i am not familiar with this type of assignment c1.type = DYNAMIC, c1.gravityScale = 0 (are they different aspects of c1? what do they do/mean?)
3)what does touch.state, BEGAN and ENDED refer to
looks like ive got alot of studying to do
thanks in advance
Ilan
@ilankess, If you want to learn more about Codea's physics classes, my suggestion whould be to review the Codea Reference. It's available from inside Codea when you select the Getting Started icon. There's a complete Physics section, which explains the functions and constants you're asking about.
@jmv38, Thanks for sharing your touch detection solution with us noobs. Your well-commented code was easy for me to follow. I really like the collision-detection method and will try to incorporate it in my projects.