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
Related
In a game I'm making I'm trying to make two Game Objects stick together on collision. I've tried making the first a child of the other, so that when the parent moves the child moves with it. But when I do that the child teleports, and his scale changes( i know it has something to do with World-location/Local-location and World-scale/Local-scale. The child's position and scale change in relative to the parent's position and scale). But I don't know how to solve it.
If anyone could help I would appreciate it.
(it doesn't have to be parent-child related, I just need a clean fix)
Reparenting is the default solution here. If you expereincing unexpected behaviour with that, its ususally a sign that you are using non-uniform scale somewhere in either of the parent chains. Best practice is to never use scales that have different x, y, z factors. If you need that to change shape of the box, make sure that you scale the box only, and have a dummy parent, to which you reparent your 'attaching' object. Having a non uniform scale somewhere up in the chain (i.e. reparenting to an object that is non uniformly scaled) will skew rotation/scale pairs down the chain, and while this might give the desired effect when only one object is involved, it may bite you when reparenting.
Alternativelty, if that fails to solve your problem for any reason, in newer versions of Unity there is a component called ParentConstraint, which should enable you to achieve the same effect
Setup:
I'm making a classic top-down, tile based puzzle game about pushing blocks and I'm trying to implement the Command Pattern to have a Undo/Redo function. In my design, a "turn" is fired whenever the player moves one tile, and the world must update somehow (lasers can be blocked, Blocks are destroyed if on top of water, Big floor buttons are pressed down, etc.)
I've abstracted my player movement into a Concrete Command MoveCommand by simply changing its position based on a direction on execution. This MoveCommand is then instantiated inside a Move() method. Blocks also happen to have a Move() method that must be called whenever the player "pushes" them (i.e. walks into them and there's a free space behind).
Question 1. Should the Block movement be its own Command, or should it be somehow appended to and managed by the player's MoveCommand? Right now I figured I would have each be its own separate thing, and then whenever the player presses the Undo button, have the Undo method execute several times until everything that must happen in that turn resolves. However, I'm not sure whether this is how you're supposed to be implementing this pattern.
Question 2 As mentioned above, blocks must be destroyed when they touch water. Should this destruction be its own WalkIntoDeadlyTile Command, or should it be appended to the Block's MoveCommand? Both questions make me realize I might not be understanding how to use this pattern, but I'm not sure how to look it up or answer these specific problems.
It is a rare case, where the ICommand interface would not be completely missplaced. You can not keep polling CanExecute in most environments. You need a way to force re-evaluation on conditions. Or just use something with binding.
But games are what I call "notorious redrawers" anyway. They could poll CanExecute once per draw pass and once in the Input processing pass, without much issues. The check would also be relatively simple (isPlayerTurn). The only possible issue is getting teh games state into the Command Implementations.
Question 2 As mentioned above, blocks must be destroyed when they touch water. Should this destruction be its own WalkIntoDeadlyTile Command, or should it be appended to the Block's MoveCommand?
It should happen in the colission check. A basic square one would work, If the block box collides with the water box, call destroy on the block. This also prevents you from forgetting to add special movement to later blocks or if the blocks somehow teleport below water.
Hi guys i was wondering how to create a shape adjustment with two objects which specifically could be described as the independent cells, one of which is static, and the second one is dynamic and surrounded by "plasma". The movement of the active object must be controllable by the user (WSAD). Collision of the active object with the static one causes the static object to be swallen, though doesn't change it's position stays in place all the time. As the active object moves, passes the swallen object and troughts it out.
See the image below:
Player character
When it comes close enough to pink enemy it's starting to swallow it (surround by yellow thing)
Pink enemy is completely sourrounded when red circle is in the centre of both.
When it leaves enemy it takes off the yellow thing
I was wondering what is the simplest way to do it. I've been thinking about cloth, physics joints, mesh substraction (is it even possible?), some kind of animation... I don't have much time to do it. Can you show me the simplest way. Which tools and approach should i use? I'm not asking for full code or full solution only for some tips.
Tim Hunter mentioned a wonderful way, most perfect in 3D.
You can use another approach in 2D :
Inside OnCollisionEnter2D try finding hit points using Collision2D.contacts . See this reference .
Create some particle effect there.
Disable the enemy
Now play swallowing animation of the player.
At animation end, enable enemy again.
Maybe calculation is little tricky, still efficient.
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 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.