Is there a detectShapeCollision() function?
-
I am looking for something like this for shapes, not sprites:
https://fuzearena.com/help/view/detectSpriteCollisionThe idea is to check collisions on joined shapes.
Because I could not find something similar, I will try do go through the array of shapes, using their coordinates.
like this https://fuzearena.com/forum/topic/477/can-you-help-me-with-shape-collisionJust let me know, if you have a better idea. Thank you ;-)
-
there is a function which can be used here to great effect I think, whilst it's not exactly detectShapeCollision(), it will make thinking about colliding your shapes much easier.
shape = createBox(0, 0, 300, 300) bounds = getShapeBounds(shape) // bounds now stores a vector of: // bounds.x = 0 // bounds.y = 0 // bounds.z = 300 // bounds.w = 300
As you can see, it returns the x, y, width and height of the shape.What isn't so helpful here is that the vector is of course labelled as an x, y, z, w vector rather than x, y, w, h. To make things clearer, you can access an element of a vector in exactly the same way you would an array:
bounds = getShapeBounds(shape) print( bounds[0] ) // to access the x component of the vector
There's no getting around the if statement you're going to write, unfortunately! It's a good idea to have a detectShapeCollision() but I can't say it would be implemented soon. I'll bring it up however!
-
Excellent hint, thanks. As soon I have access to my NS, I will try the following code. I tried to implement the pseudo code from the polygon test after Jordan.
Appendix 17.10.2019: fixed the code, for checking a point inside a polygon.
// returns: false if p is not inside the polygon // true if p is inside the polygon function detectPolyCollision(points, p) result = false t = -1 n = len(points) for i = 0 to n-1 loop t = t * crossProdTest(p, points[i], points[i+1]) if t == 0 then result = true break endif repeat if t == -1 then result = false else result = true endif return result // returns: -1 if point A is outside the polygon // 0 if point A is laying on an edge // 1 if point A is inside the polygon function crossProdTest(A, B , C) t = 0 if (B.y > C.Y) then // swap B with C pt = {B.x, B.y} B = {C.x, C.y} C = {pt.x, pt.y} endif if (A.y <= B.y) or (A.y > C.y) then t = 1 else delta = (B.x - A.x) * (C.y -A.y) - (B.y - A.y) * (C.x - A.x) if delta > 0 then t = 1 else if delta < 0 then t = -1 else t = 0 endif endif endif return t
Example:
polygon_points = [ {920, 520}, {1020,520}, {1120,420}, {1220,520}, {1320,520}, {1320,560}, {920,560}, {920,520} ] p = {1050,500} collision = detectPolyCollision(polygon_points, p)
-
It wont become as nice, as I thought: getShapeBounds(shape) returns a "rectangle" around the "joined or not joined" shape (of course, like you described it). Not a polygon as I would need it.
It seems like I cannot extract shape coordinates from a joined shape again or get all edges of it.
So, I probably will not be able to use the joinShapes() function and handle the shapes one by one. -
Rather than joining multiple shapes could you use createPoly() to make a single shape with the same outline? Then you would have all the points for the polygon, because you would have fed them into the function in the first place.
-
I think currently your absolutely right, @Richard. So, I currently create a shape with the nice colored joined shapes and at the same time I keep an array of points, describing a polygon around it (that I move and rotate according to the joined shapes) to detect precise collisions.
For this I adapted the collision detection code above to become the detectPolyCollision() function.But this works now only for point-to-shape collision detection (or for circle shapes to any other shape collision detection). What for my current code project is enough. But I still dream of a detectShapeCollision() ;-)
But that can probably only be solved nicely (without duplicate data), when there is a function to unjoin shapes and get back the coordinates, or just directly the polygon coordinates of any shape. -
Shared the code for the detection of a point with a polygon on SW-4464-9632-0709.