Math Profis this is for you...

ilan

Expert
Licensed User
Longtime User
hi everyone

i need some help.

i would like to know if a touch (x/y) is on the edge of an shape.

rt1.png


now this is simple i just calculate if the touch is bigger or smaller then centerPoint + shape.width/3

now that is working BUT how do i do it if the the shape is rotated??

rt2.png



thanx
 
Last edited:

sorex

Expert
Licensed User
Longtime User
ok i solved it. i just calculate the distance between both points and if it is > then x i know the edges where touched.

won't this fail if the object is 90° or 270° rotated? then they are in line.
 

wonder

Expert
Licensed User
Longtime User
I assumed that the complete algorithm would be:
Step 1) Verify that:
- userTouch(x, y) is inside Polygon[list(x,y)]
Step 2) Verify that:
- abs(distanceSquared(PolygonCenter, userTouch)) > abs(distanceSquared(PolygonCenter, SpecificPoint))
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Another algorihtm:
Untitled.png

Create two copies of one HitBox[A, B, C, D] and offset its position (center point) by:
(-Polygon.halfWidth + HitBox.halfWidth) for box A, and
(Polygon.halfWidth - HitBox.halfWidth) for box B.

Verify:
- UserTouch(x,y) is inside BoxA[] or inside BoxB[]

 
Last edited:

ilan

Expert
Licensed User
Longtime User
Another algorithm :p

actually i solved it in a different way.

i create 2 radius lengths:

1. movingRadius
2. rotatingRadius

now what i do is i check the distance between touch position (x,y) and center position (x/y) of the object.

if it is < movingRadius then object starts to move else if its < rotatingradius obj starts to rotate else (to far away from object = do nothing)


alg1.png


like this its more user friendly to move an object. instead of having the touch to be inside the physicbody now it can be outside but still filter unwanted touches.

the movingradius and rotatingradius has also a minimum value so if i use small objects you still get a good touch handling.
so i set the radius according to the object width or height (the bigger of both) like:

(lets take the rotating radius for example)

B4X:
rotatingradius = max(max(obj.width/2,obj1.height/2),vpW*0.1)

so the rotating radius is the size of the bigger value between (obj.width/2 and obj.height/2) BUT NOT smaller then vpW*0.1 (10% of the screen width)

this is how i do it in my new iSpriteKit game.
 

ilan

Expert
Licensed User
Longtime User
won't this fail if the object is 90° or 270° rotated? then they are in line.

distance takes account both x and y axis so why should there be a problem if you have 90' or 270'?

dist.png


B4X:
Sub distance(x1 As Float, x2 As Float, y1 As Float, y2 As Float) As Double
    Return Sqrt(Power(x2-x1,2)+Power(y2-y1,2)) 'simple distance calculation
End Sub
 

sorex

Expert
Licensed User
Longtime User
distance takes account both x and y axis so why should there be a problem if you have 90' or 270'?

I misinterpret this line

"i just calculate the distance between both points and if it is > then x"

I thought you only checked x position distance.
 
Top