[cairo] Check collision via cairo_clip_extents

cecashon at aol.com cecashon at aol.com
Sat Nov 24 19:03:50 UTC 2018

Hi all,

Not sure how to go about this myself. It seems that it would be best to detect two boxes overlapping first and then refine the search looking at if the two shapes overlap. With some linear algebra you should be able to find if a point from the triangle is in the rectangle or a point from the rectangle is in the triangle.

I got the overlapping boxes working but haven't added the refinement.



If you are building a collision library I suspect some shapes are going to require a fair amount of math to figure out where they intersect. Sounds like an interesting project.

-----Original Message-----
From: yanghong <yanghong at thundersoft.com>
To: Uli Schlachter <psychon at znc.in>
Cc: cairo <cairo at cairographics.org>
Sent: Fri, Nov 23, 2018 3:57 pm
Subject: Re: [cairo] Check collision via cairo_clip_extents

Hi Uli

Sorry for the confusion, the size of the triangle/rectangle is pre-defined by the length of edges, and I want to scale them dynamic. I'd use cairo_scsle() here.

I want to check the collision between the triangle and the rectangle when move them on a cavas, it's a 2D game.

Please check the attached txt file in previous email, I want get the bounding of the C area. Take a look at the first composition of the triangle/rectangle in the image.png output by the cairo_sandbox.c, The small grey triangle is the C area which is the intersection of the triangle/rectangle. cairo_clip_extents return a rec as height as the triangle, it's higher than the grey triangle.

Hope the question is clear now:) I'm using Racket to write this game in Ubuntu 16.04, cairo is the backend of the drawing API, so I try to find such API in cairo.

Best regards
Hong Yang

#yiv1303904544 * body {background-color:#ffffff;line-height:1.4;word-wrap:break-word;}#yiv1303904544 div {word-wrap:break-word;}#yiv1303904544 p {word-wrap:break-word;text-indent:0pt !important;}#yiv1303904544 span {word-wrap:break-word;}#yiv1303904544 a {word-wrap:break-word;}#yiv1303904544 td {word-wrap:break-word;}Uli Schlachter <psychon at znc.in>于 2018年11月24日 上午12:54写道:


first of all, your scale parameter confused me a lot. :-)

Then: I do not really think that cairo clip extents is the right API for
collision detection, but okay...

On 23.11.18 10:16, Yang Hong wrote:
> I tried to use cairo_clip_extents() to find intersect of triangle and
> rectangle, but it doesn't work, because the returned bounding is bigger than
> the accurate intersect. 

I fail to see this "too big". What's the output that you want to get
from this?

Let's look at each printf:

> cairo_new_path(cr);
> create_triangle(cr, rec36, ARRAY_SIZE(rec36), 1);
> cairo_clip(cr);
> cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
> //cairo_fill_extents(cr, &x1, &y1, &x2, &y2);
> printf("Clip extents: %f, %f, %f, %f\n", x1, y1, x2, y2);

This prints 0 0 60 120 for me (well, with lots of trailing ".0000"). And
indeed, rec36 contains the points (0,0), (3,0), (3,6), (0,6). The
extents of this are (0,0) to (3,6), which after multiplying by scale=20
gives (0,0) and (60,120), which is just what cairo is giving you.

> cairo_reset_clip(cr);
> cairo_new_path(cr);
> create_triangle(cr, tri4, ARRAY_SIZE(tri4), 1);
> cairo_clip(cr);

tri4 has the points (3,0), (6,3), (0,3). The extents of this are (0,0)
to (6,3).

> cairo_translate(cr, 100, 0);
> cairo_new_path(cr);
> create_triangle(cr, rec36, ARRAY_SIZE(rec36), 1);
> x1 = y1 = x2 = y2 = 0.0;
> cairo_clip(cr);
> cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
> //cairo_fill_extents(cr, &x1, &y1, &x2, &y2);
> printf("Clip extents: %f, %f, %f, %f\n", x1, y1, x2, y2);

Okay... output for me here is 0 0 20 60.
Extents of tri4 are (-5,0) to (1,3) (because of the translate by (100,0)
above, which is (5,0) when divided by scale=20).
Extents of rec36 are (0,0) to (3,6).

The intersection of these to is from (0,0) to (1,3) (and all of this is
actually "touched" by one of the shapes, so the extents here are right,
as far as I can see). After multiplying by scale=20, this is exactly the
numbers you get here.

[etc for the other cases; I got a bit bored here].

So, I fail to understand the problem. Sorry.

Could you perhaps send an example that actually just reproduces the
problem and explain why you think that cairo should produce different
values here (and what those values are)?

Bitte nicht mit dem verbleibenden Auge in den Laser gucken.
 - Vincent Ebert

cairo mailing list
cairo at cairographics.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20181124/5f3d0638/attachment.html>

More information about the cairo mailing list