I have a canvas in Qt (QGraphicsScene in QGraphicsView) on which user can add shapes: circle, square, rectangle, ellipse and triangle and change the size of either (as single QGraphicsObject subclass). Between these shapes user can create lines and the direction of the line is indicated by drawing an arrow at the intersection point of the line and a shape in the similar manner to Qt's example ElasticNodes (the connection is also QGraphicsObject suclass).
Now in order to support bi-directional connections and multiple connections of the same shapes I change any subsequent connection line into quadratic curve with progressively bigger arc to allow their selection and visualisation.
The implementation of the shapes is so that their centre is NOT top left, but instead the centre of the shape.
The connection between them is a line between these two centres pushed back in Z axis so it hides behind the shapes.
The arrow is position at the edge on a shape is determined depending on a shape: radius for circle and trigonometrics for other shapes.
Now when using quadratic curve I need to reposition the arrow to the intersection point of the curve and a shape. With that point I can then use the same procedure to render the arrow because I can get the angle at certain point from QPainterPath.
However he biggest challenge is to detect this collision point. The only option I can think of is to use QPainterPath::intersected but for that to work it requires fill areas (i.e. making the curve with a width of at least 2) and I would still need to somehow extract the correct point from the result - not sure how yet.
I would appreciate any ideas on how I could go about this.
Related
I am creating a game in qt Creator using c++ and OpenGL and an attempting to add bounding boxes to my scene in order to implement collision detection. I am using objects imported from Maya as .obj in my scene so their dimensions are not set in the code, only their position, rotation and scale. I am able to create a bounding box around each object which matches their position but am struggling to find a way to access the min and max x, y and z values of the objects in order to match the box to the size of the object.
Does anyone have any ideas on how I could access the min and max coordinates? I know how to implement the code if I could access these values..
The problem you afford is that each object geometry has different means of internal storage and determination of a bounding box.
Let's try some examples to illustrate this:
Suppose we have a circle, whose drawing parameters stored internally are the center coordinates x_center and y_center and the radius radius. If you try to determine the bounding box for this object, you'll see that it extends from (x_center - radius, y_center - radius) to (x_center + radius, y_center + radius).
In case you have an unrotated rectangle, given by the two points of it's principal diagonal, the bounding box just coincides with it's shape, so you have only to give the coordinates of the two same points that represent it.
If, on the other way, we have a polygon, the bounding box will be determined by the minimum and maximum coordinates of all the polygon vertices. If you allow to rotate the polygon, you'll need to rotate all the vertices coordinates before determining their maximum and minimum values, to get the bounding box.
If, for another example, we have a cubic spline, determined by the coordinates of its four control points you'll be determining the maximum and minimum values of two cubic polygons, which means solving two quadratic equations(after derivation), in the general case.
To cope with all this stuff, a geometric shape normally includes some means of polymorphically construct it's bounding box (it normally is even cached, so you don't have to calculate it, only after rotations or variations in it's position or scale) via some instance method.
Of course, all of this depends on how and how has defined the way shapes are implemented. perhaps your case is simpler than I'm exposing here, but you don't say. You also don't show any code or input/output data, as stated in the How to create a Minimal, Complete, and Verifiable example page. So you had better to edit your question and add your sample code, that will show more information about your exact problem.
if you have obj loader so you have an array.
float t[2100];
int x = 2100;
float xmax=-123243;
while(x>=0)
{
if(xmax<t[x]) xmax=t[x];
x-=3;
}
So here is a maximum x of the object(?).
I have a rotated Rectangle inside a closed tile map of rectangles.
What would be the best way to check if the player (which is the rotated rectangle) is intersecting with one of the rectangles of the tile map?
Picture from inside the game to better show what the map looks like:
If it matters, the Player's type is sf::Shape and the map's data is inside an int array.
SFML does not provide collision detection, it only has method to check if two axis-aligned rectangles intersect. If you need something more complex, you will have to implement if yourself.
If you don't need precision detection, you can test Sprite.getGlobalBounds().intersects(...) with the rectangle of the map.
If you want ideal collision detection, you have more then one option:
Pixel perfect Collision. First check if bounding box intersect the map tile and them check all non-transparent pixels for collision. Not very fast but easy to implement and may be suitable for your case.
Mathematical methods, there are more that one, but take a look at Separating Axis Theorem. If your are only limited to rectangles (or/and circles and convex polygons), it will work best.
For anyone still having this issue:
You should look into the getTransform() and getInverseTransform() functions of sf::Transformable (https://www.sfmldev.org/documentation/2.5.1/classsf_1_1Transformable.php). Getting the inverse transforms of the player and a specific wall allows you to use a simple AABB collision algorithm (like SFML already implemented it in getGlobalBounds().intersects(...)). You basicly look at the local coordinate system of the player and how the wall is positioned to it, all translations, rotations and scaling ignored.
Im working on a 2D game in which the terrain can vary and is composed of any shape of polygons except for self intersecting ones. The player collision box is in the shape of a square and can move about. My question is this: How do I keep an always-upright box to collide with variable terrain and always stay outside?
My current approach that I made up albeit no code yet works like the following:
The blue square is the player hitbox. First, it moves with a velocity downwards as an example. My goal is to find the heighest point in its travel path where it can be safely outside of the terrain polygon. I test all the terrain vertex points inside its travel path and project them to the velocity of the box. I take the farthest projection.
The farthest projection will be the max distance allowed to move in without going into the terrain.
Move the square by distance in the direction of velocity and done.
However, there are few scenarios that I encountered where this does not work. Take this as an example:
To remedy this situation, I now test for one corner of the square. If the distance from the corner is shorter than the farthest projection, then that distance will give the appropriate shift in distance. This pretty much makes the algorithm full-proof. Unless someone states another exception.
Im going a little crazy and I would appreciate feedback on my algorithm. If anyone has any suggestions or good reads about 2D upright box collisions on terrain or anything similar, that would be great.
This may be useful, and here I'll quickly elaborate on "upright" square collision.
First the collision may occur on the side of the square, and not necessarily a corner. A simple solution to check any collision is describe the region delimited by the square, and then check if any point of your uneven terrain is within this region.
To define the square region, assume your upright square is has the corners (x1,y1), (x2,y1), (x2,y2), (x1,y2), where x2>x1 and y2>y1. Then for a point (x,y) to be within the square it needs to satisfy the conditions
If( x1< x < x2 and y1< y <y2) Then (x,y) is in the square.
Then to conclude, all you need do is check if any point on the terrain satisfies the above condition.
Good luck.
I have a sprite with a triangle shape and I want to know when this triangle intersects with another object (a CCSPrite). I have been using CGRectIntersectsRect but it is not accurate because it take the bounding box of the the two sprites and not the actual shape.
--Edit
I think one way is to define several points around the actual triangle and check intersection between those points with another sprite. I am just wondering if there is an easier way to do this.
You can use box2d to detect collisions accurately. It can be useful if you have many different complicated shapes. Or you can just check intersections of shape's edges.
If there is many objects to detect collisions with, I offer to use box2d. It has good internal optimizations to be able to work with large amount of objects. In this case you will just have to create physical body equal to the your sprite's shape before adding object to your game layer.
I am working on a simple mesh viewer implementation in C++ with basic functionality such as translation, rotation, scaling.
I'm stuck with with implementing the rotation of the object along z-axis using the mouse. What I want to implement is the following:
Click and drag the mouse vertically (almost vertical will do, as I use a simple threshold to filter slight deviations along the horizontal axis) to rotate the object along y-axis (this part is done).
Click and drag the mouse horizontally just as described above to rotate the object along x-axis (this part is done too).
For z-axis rotation, I want to detect a circular (or along an arc) mouse movement. I'm stuck with this part, and don't know how to implement this.
For the above two, i just use atan2() to determine the angle of movement. But how do I detect circular movements?
The only way to deal with this is to have a delay between the user starting to make the motion and the object rotating:
When user clicks and begins to move the mouse you need to determine if its going to become a straight line movement, or a circular one. This will require a certain amount of data to be collected before that judgement can be made.
The most extreme case would be requiring the user to make one complete circle first, then the rotation begins (in reality you could do much better than this). Just how small you are able to cut this period down to will depend on a) how precise you dictate your users actions must be, and b) how good you are with pattern recognition algorithms.
To get you started heres an outline of an extremely poor algorithm:
On user click store the x and y coordinates.
Every 1/10 of a second store the new coordinates and process_for_pattern.
in process_for_pattern you're looking for:
A period where the x coordinates and the y coordinates regularly both increase, both decrease, or one increases and one decreases. Over time if this pattern changes such that either the x or the y begins to reverse whilst the other continues as it was, then at that moment you can be fairly sure you've got a circle.
This algorithm would require the user to draw a quarter circle before it was detected, and it does not account for size, direction, or largely irregular movements.
If you really want to continue with this method you can get a much better algorithm, but you might want to reconsider your control method.
Perhaps, you should define a screen region (e.g. at window boundaries), which, when was clicked, will initiate arc movement - or use some other modifier, a button or whatever.
Then at a mouse click you capture the coordinates and center of rotation (mesh axis) in 2D screen space. This gets you a vector (mesh center, button down pos)
On every mouse move you calculate a new vector (mesh center, mouse pos) and the angle between the two vectors is the angle of rotation.
I don't think it works like that...
You could convert mouse wheel rotation to z-axis, or use quaternion camera orientation, which is able to rotate along every axis almost intuitively...
The opposite is true for quarternion camera: if one tries to rotate the mesh along a straight line, the mesh appears to rotate slightly around some other weird axis -- and to compensate that, one intuitively tries to follow some slightly curved trajectory.
It's not exactly what you want, but should come close enough.
Choose a circular region within which your movements numbered 1 and 2 work as described (in the picture this would be some region that is smaller than the red circle. However, when the user clicks outside the circular region, you save the initial click position (shown in green). This defines a point which has a certain angle relative to the x-axis of your screen (you can find this easily with some trig), and it also defines the radius of the circle on which the user is working (in red). The release of the mouse adds a second point (blue). You then find the angle this point has relative to the center of the screen and the x-axis (just like before). You then project that angle onto your circle with the radius determined by the first click. The dark red arc defines the amount of rotation of the model.
This should be enough to get you started.
That will not be a good input method, I think. Because you will always need some travel distance to discriminate between a line and a curve, which means some input delay. Here is an alternative:
Only vertical mouse having their line crossing the center of the screen are considered vertical. Same for horizontal. In other cases it's considered a rotation, and to calculate its amplitude, calculate the angle between the last mouse location and the current location relatively to the center of the screen.
Alternatively you could use the center of the selected mesh if your application works like that.
You can't detect the "circular, along an arc" mouse movement with anywhere near the precision needed for 3d model viewing. What you want is something like this: http://thetechartist.com/?p=80
You nominate an axis (x, y, or z) using either keyboard shortcuts or on-screen axis indicators that you can grab with the mouse.
This will be much more precise than trying to detect an "arc" gesture. Any "arc" recognition would necessarily involve a delay while you accumulate enough mouse samples to decide whether an arc gesture has begun or not. Gesture recognition like this is non-trivial (I've done some gesture work with the Wii-mote). Similarly, even your simple "vertical" and "horizontal" mouse movement detection will require a delay for the same reason. Any "simple threshold to filter slight deviations" will make it feel dampened and weird.
For 3d viewing you want 1:1 mouse responsiveness, and that means just explicitly nominating an axis with a shortcut key or UI etc. For x-axis rotation, just restrict it to mouse x, y-axis to mouse y if you like. For z you could similarly restrict to x or y mouse input, or just take the total 2d mouse distance travelled. It depends what feels nicest to you.
As an alternative, you could try coding up support for a 3D mouse like the 3dConnexion SpaceExplorer.