I am making a simple application for win 8, In which user can move controls (BUTTONS etc), How to detect when buttons collide each other and how to detect that button is colliding with inner boundary of screen? Kindly Help me with this !
From my game programming experience, I would imagine you would do a check using the position and size of both objects to determine if there is an intersection, and hence, a collision taking place.
Probably do that on the object moved event. Cycle through your entire collection of objects and do the intersection check.
I am not quite sure what you are trying to achieve and haven't got windows 8, but the principle should still be valid.
Related
NOTE: Please be sure to read the Let us continue this discussion in chat for background notes
I have seen a few posts on here and tutorials for checking to see if controls overlap but what is the best method for checking this during an animation?
I'm making a simulation software which involves having some UIElements animate along a path. At the moment I have 20 items following this path and it works fine.
To do this I just create a loop of 20 and inside the loop, I am creating the UIElement, storyboard etc and then starting. I wait for about 100ms then repeat. This gives a nice gap between the elements.
Anyway, the above works as it should. Now the next bit. At any point of the sim, a UIElement can stop where it is. Now when this happens, I want the other UIElements to keep going until they hit the stopped element, and 1 by 1 they stack up behind it.
So knowing how to check for overlap/intersection/collision, how do I check during an animation of elements.
The collision detection, by its nature, needs a constantly checking timer. because there are no collision events in WPF.
You can check for the collision of two Rect objects with rect1.IntersectsWith(rect2)
taken from here
You can find the Rect of an element relative to its parent using
public static Rect BoundsRelativeTo(this FrameworkElement element,
Visual relativeTo)
{
return
element.TransformToVisual(relativeTo)
.TransformBounds(LayoutInformation.GetLayoutSlot(element));
}
taken from here
The question is how many elements are being dropped into the bottleneck of the timer at a time, if the collision detection ALWAYS involves a stopped element, then that element is responsible for collision detection. Otherwise, you will need to check n^2 collisions
(which is bad). You can work on the efficiency of this bottleneck by reducing the number of comparisons as much as possible. For example, since the objects are moving along a common path you can compare the stopped element against its previous element only. You can ensure a sorted collection of elements to achieve that.
Since all elements are following a common path (one collision occurs at a time):
start the timer when the first element is stopped. and set it as the reference element
compare all required elements against the reference element
change the reference element when a collision occurs to the collided element
So I created this window editor in WPF that helps me create Forms quickly. Now, one feature I've worked on was create a guideline tool. At its core it's just creates lines to help keep my UI elements organized on the screen. I will show you an example. The long black lines are the guidelines I spoke about earlier.
Now, I noticed that in a lot of art programs (i.e Photoshop) and popular IDEs that implement Forms that they have a "snap-to" feature where a UI element will snap to a line UI or to another UI element in order to maintain alignment. Something like this:
I already have the guidelines showing up in my editor. Now, what I would like help understanding is, how would I go about implementing the "snap to" feature? I'n not asking for code, just a breakdown (a visual breakdown will be most welcomed).
These are my questions:
How does an object know if one of its edges (top, bottom, left, right) touched a line?
How would I know how to unsnap the UI element if the user keeps moving the mouse past the guideline?
If I have (say) 10 lines how do I make sure that the object attaches to the nearest line(s)?
UPDATE
When an object moves or is resized, keep track of its actual size/location relative to the mouse, and separately keep track of a snapped version of the same information. If a given actual edge is within some arbitrary distance of a line -- say 4 pixels (arbitrary WPF units, really). If it's within that distance, set it to the value for the line it's close to. You still have the actual mouse-relative values as well, so you know to unsnap it if the the user keeps on dragging it and it leaves that 4-unit zone.
When an object is being resized, at most two edges of the bounding box will be changing position (assuming you can drag corners as well as edges). When you're moving an object, all four edges of the bounding box will move.
So you need to keep track of which edges are moving, and only do snap-line proximity testing on those edges. When you're moving an object, snapping the left or top edge to a line is easy. That's just the position of the object. But if you snap the right or top edge to a line, you're setting
snappedPos.X = nearestVerticalSnapLine.X - draggedObject.Width;
or
snappedPos.Y = nearestHorizontalSnapLine.Y - draggedObject.Height;
You may also have cases where opposite edges will both be in proximity to lines: Say you're dragging a seven-unit square across a ten-unit grid. When it's inside a grid box, all four sides will be in proximity to a grid line. Which wins? The closer one.
Locating the snap lines is easy -- %.
I have two cubes with one being slightly bigger than the other and can be moved around freely around the scene by the user.
I need to know when the user has placed the smaller one on top of the bigger one and then continue with the script.
I understand that I need to check the collision somehow and maybe check the Y coordinate of both boxes too to make sure the user puts the smaller cube on top of the bigger and not vice versa.
How can I do this?
You could either create a trigger (more on that in the unity docs) and use the respecrive callback to continue or you could constantly check the position of the second object and if the coordinates are above the ones of the big box continue.
I am developing a game with Unity which has a main menu to allow the user select game mode. In this screenshot you can see an example:
My problem is the following:
I need to create a scroll in which user can put his finger on point 1 and swipe/slide to point 2. On the one hand, while this action is happening the element 2 increase its opacity and its position change progressively to the center. On the other hand, while this action is happening the element 1 decrease its opacity and its position change progressively to the left part of the screen.
Extra information:
The elements are sprites with colliders to detect the selection of the player.
What would you suggest me to do? Have you got any code to solve this? Any other suggestion?
Hey i think i understood your problem,
According to my understanding. You can use a binary tree for this work, u can use a sprite sheet with different level of alpha value. The variable of the binary tree responsible for changing the sprite will depend on the finger's position on the screen which you can easily get by camera.ScreenToWorldPoint(pos). also you can easily change the position of the gameObject by recording the change in initial and final position of the touchPhase.
Say I have multiple objects laying on each other, if I click on one of them, the coordinates returned from TouchPanel.GetState() would not able to tell me which object is selected. The object is not necessary the top most one. In this case, do I organize the list of objects so that sorted by Z value and compare Rectangles from top to bottom. Is this right way to do that? Or if there is any UI framework providing some sort of component to inherit so that the object will have callback when there is external event happening?
If you're using the XNA Game Libraries, you're going to have to use a Z value to determine which object was selected.
XNA's TouchPanel.GetState() will only return the position of the fingers on the screen, it won't tell you which objects it's colliding with. You have to do all that collision checking yourself, which is where you can decide via Z or layering order, which object you'd like to select.
For 2D objects, there isn't any specific Z order applied to them (because they're 2D, not 3D), so you'll need to add a property to handle that to your object's class.
Then when you touch a position, you can collect all of the objects that the position collided with and select whichever object you want based on it's z order.
Hope that helps!
This XNA sample should get you started:
http://create.msdn.com/en-US/education/catalog/sample/picking
As Nick Funk said, you have the 2D touch coordinates, then it's just a matter of applying the above sample to your game and using those coordinates.