Is there a way to get the current position of a famo.us Surface?
I mean, after the render tree has done its thing and all modifiers have been multiplied, the surface exists somewhere on the screen. But I can't figure out how to just ask where. Seems a simple enough question.
Specifically, I'm building a kind of scroll view and need to test if a contained surface has moved outside of a bounding box.
It looks like the surface itself has a _matrix property that I could use, but this seems a crude way of doing it.
What am I missing?
Related
Using moveto and lineto to draw various lines on a window canvas...
What is the simplest way to determine at run-time if an object, like a bit map or a picture control is in "contact" (same x,y coordinates) with a line(s) that had been drawn with lineto on a window canvas?
A simple example would be a ball (bitmap or picture) "contacting" a drawn border and rebounding... What is the easiest way to know if "contact" occurs between the object, picture or bitmap and any line that exists on the window?
If I get it right you want collision detection/avoidance between circular object and line(s) while moving. There are more option to do this I know of...
Vector approach
you need to remember all the rendered stuff in vector form too so you need list of all rendered lines, objects etc ... Then for particular object loop through all the other ones and check for collision algebraically with vector math. Like detecting intersection between bounding boxes and then with particular line/polyline/polygon or what ever.
Raster approach
This is simpler to mplement and sometimes even faster but less acurate (only pixel precision). The idea is to clear object last position with background color. Then check all the pixels that would be rendered at new position and if no other than background color present then no colision occurs so you can render the pixels. If any non background color present then render the object on the original position again as collision occur.
You can also check between old and new position and place the object on first non collision position so you are closer to the edge...
This approach need fast pixel access otherwise it woul dbe too slow. Standard Canvas does not allow this without using BitBlt from GDI. Luckily VCL GRaphics::TBitmap has ScanLine[] property allowing direct pixel access without any performance hit if used right. See example of it in your other question I answered:
bitmap rotate using direct pixel access
accessing ScanLine[y][x] is as slow as Pixels[x][y] but you can store all the pointers to each line of bitmap once and then just use that instead which is the same as accessing your own 2D array. So you really need just bitmap->Height calls of ScanLine[y] for entire image rendering after any resize or assigment of bitmap...
If you got tile based scene you can use this approach on tiles instead of pixels something like this:
What is the best way to move an object on the screen? but it is in asm ...
Field approach
This one is also considered to be a vector approach but does not require collision checks. Instead each object creates repulsive force the bigger the closer you are to it which is added to the Newton/D'Alembert physics driving force. When coefficients set properly it will avoid collisions on its own. This is used also for automatic placement of items etc... for more info see:
How to implement a constraint solver for 2-D geometry?
Hybrid approach
You can combine any of the above approaches together to better suite your needs. For example see:
Path generation for non-intersecting disc movement on a plane
I created a flowchart in InkScape in which each shape is filled with a linear gradient.
Some shapes share the same gradient tones but when I created them via copy&past did not know that gradients could be transformed during edit, I thought that copy&pasting a shape simply copy the entire object and each colors.
Anyway, it resulted in shapes that changes background gradient during re-positioning. After several searches I discovered that this is caused by a wrong position of "Gradient Editor Tool" of each one. In other words I moved objects but start/end of each gradient remained in place.
Please, how to quickly reposition gradient direction/start/end of each shape, maybe centering it on each one? I would avoid to manually reposition each one...
Thanks
Inkscape provides a toggle button to move the gradients along with the objects.
Activating this button during any coying or moving carries the gradient with it to the new location. (Actually it copies the gradient.)
I have some problems with the intersection functionality in SFML when I am resizing the window.
So I do fairly know how to detect intersections or if something is clicked and so on when the window is in the predefined size.
But when resizing the window, the golbal bounds of the shapes/sprites in sfml stay exactly the same while their presentation in the window changes.
So when I now click on something it may happen that the normal SFML contains method of an object tells me that the mouse pointer is not inside, even if it seems to be like that on the screen.
The only thing I have in mind is to have a variable (e.g sf::vector2f) that stores the current change of the window compared to the original size and then not use the mouse position relative to the current window but the (with the change multiplied) projected mouse position.
But this may not be the best solution, so I wonder if I am missing something and therefore I am asking for advice, what to do here?
You can use the sf::RenderWindow::mapPixelToCoords method to find out the correct position of the mouse.
From the SFML documentation:
Convert a point from target coordinates to world coordinates.
This function finds the 2D position that matches the given pixel of
the render target. In other words, it does the inverse of what the
graphics card does, to find the initial position of a rendered pixel.
Initially, both coordinate systems (world units and target pixels)
match perfectly. But if you define a custom view or resize your render
target, this assertion is not true anymore, i.e. a point located at
(10, 50) in your render target may map to the point (150, 75) in your
2D world – if the view is translated by (140, 25).
For render-windows, this function is typically used to find which
point (or object) is located below the mouse cursor.
This version uses a custom view for calculations, see the other
overload of the function if you want to use the current view of the
render target.
For the following code segement, my problem is that the two objects are intersected, but the views (lower figure) are not correct, object 1 (box) is inserted into the cylinder but the sideview (lower figure) is not correct, it looks like the yellow box is behind the cylinder. How can I make it look they are intersected?
glColor3f(1,1,0);
drawobj1(); // draw box
glColor3f(1,0.5,0);
drawobj2();draw Cyclinder() using gluCylinder
It is behind the cylinder. It is both inside and behind it. Part of the box is inside it, and part of it is behind it.
Imagine a fork embedded in the side of a can. You can rotate the can so that it appears like the cylinder in your diagram. The fork is still embedded in it, but from that angle, you can only suspect that it is based on what you know about the length of a fork.
Your problem is the lack of visual depth cues, brought on by the fact that this scene lacks lighting, textures, and everything else that your brain normally would use to actually interpret something.
I have been able to find a lot of information on actual logic development for games. I would really like to make a card game, but I just dont understand how, based on the mouse position, an object can be selected (or atleast the proper way) First I thought of bounding box checking but not all my bitmaps are rectangles. Then I thought f making a hidden buffer wih each object having a different color, but it seems ridiculous to have to do it this way. I'm wondering how it is really done. For example, how does Adobe Flash know the object under the mouse?
Thanks
Your question is how to tell if the mouse is above a non-rectangular bitmap. I am assuming all your bitmaps are really rectangular, but they have transparent regions. You must already somehow be able to tell which part of your (rectangular) bitmap is transparent, depending on the scheme you use (e.g. if you designate a color as transparent or if you use a bit mask). You will also know the z-order (layering) of bitmaps on your canvas. Then when you detect a click at position (x,y), you need to find the list of rectangular bitmaps that span over that pixel. Sort them by z-order and for each one check whether the pixel is transparent or not. If yes, move on to the next bitmap. If no, then this is the selected bitmap.
Or you may use geometric solution. You should store / manage the geometry of the card / item. For example a list of shapes like circles, rectangles.
Maybe triangles or ellipses if you have lots of time. Telling that a triangle has a point or not is a mathematical question and can be numerically unstable if the triangle is very thin (algorithm has a dividing).. Fix: How to determine if a point is in a 2D triangle?
I voted for abc.