Best approach to animate physics in Silverlight? - silverlight

I am new to Silverlight and want to animate a ball that is shot out of a cannon then moves through the air under the laws of physics (so roughly, on an elliptical path).
My inclination is to use a callback timer and move the little ball every 50ms by changing its Canvas.LeftProperty and Canvas.TopProperty values. Is this the right approach or should I be using a DoubleAnimation? My resistance to using animations here is that I would have to create many different animation in succession.
Which path would a Silverlight guru take?

Have a look at a question I asked some time ago here the accepted answer gives a comparison of five different ways to do animation for games in silverlight linking to this blog. The article concludes that the method you choose will depend on a number of factors.
To answer you question I personally would use the dispatcher timer which is fired each time the screen re-draws and set the properties then to move the X and Y.
I also found Game Physics 101 a really good resource.

Might be a little overkill bit are you aware of the physics helper libraray

I don't think you would have to create several animations in succession. The ball only changes direction once.
The Y value goes down (making the ball look like it's going up) and then goes up (making the ball look like it's going down). The deceleration as it's going down should be constant (something representing 9.8 m/s/s) and the acceleration as the Y value goes up (cannonball goes down) is constant, until it hits the point representing the ground.
The X value should be constant, or else decelerate slightly should air resistance be taken into account.
Therefore, you would work out the following:
Where does it start (the cannon,
obviously)
Where does the ball hit it's vertical peak
Where does the ball hit the ground
So now you can animate the Y value as a start value, and end value and one waypoint.
And you can animate the X value with just a start and an end.
Of course, it's going to involve non-trivial mathematics to determine the exact numbers to enter into the AccelerationRatio, DecelerationRatio, SpeedRatio, Duration, From and To parameters. But you would have to work that out no matter what method you're using.

Related

Pinch and Rotate Events are called at the same time no matter finger position in Unreal?

Basically, I am creating a mobile app in Unreal. I found that Unreal has built in Rotate and Pinch gestures for mobile phones. However, about five seconds after I discovered them I once again fell into the abyss that is despair.
After attaching just a simple print string to both events I found that no matter how I place my fingers on the screen both events are called basically at the same time. Setting up simple Booleans for each event I found that the pinch event basically wins out in this input battle 9/10 times.
I feel like I'm not doing anything wrong as far as programming goes considering it is a built in event. Is this some sort of bug or is it broken? Is there some specific way my fingers have to move in order to call one event or the other? I feel like I did the pretty standard pinch and rotate gesture for mobile phones...
After doing some more research I found that after setting up an axis value for the two gesture events, they both give very different values. The pinch gesture gives a value consistently between 0 and 14ish. On the other hand the rotate gesture axis returns a degree of rotation.
I solved the issue of both of them performing at the same time by caching the current rotation axis value and checking it against the next value if the difference between the two was greater than 10 (Absolute value) then I performed a rotate, I also took that value and divided it by 5 to get a smoother rotation, 2 degrees per call.
As far as the pinch axis goes, I didn't feel the need to put a limiter on it because my zoom speed is relatively slow and for the most part when performing the rotate the value doesn't change very much. However, if anyone plans on using the pinch gesture, I highly recommend normalizing the range between -1 and 1. I think it is just poor execution on Unreal's part that it doesn't do this out of the box. A little suggestion on how to do this is that you'll have to check if the value is greater than 1 (Zoom in) or not (Zoom out). So you have to normalize the values from 0-1 to -1 to 0 and the values 1-14 to 0 to 1 if that makes sense. Considering that the pressed event is called every tick that two fingers are on the screen it just makes sense to normalize the values. Of course if you're just replacing the scale with the axis value then it works out of the box but, most people don't scale the objects up they just move the camera/boom forward or smaller.
I hope this helps somebody else trying to use these poorly documented gesture events.

Create GO like AI - maybe with minimax algorithm?

So I'm trying to make a game that's similar to the game GO. Essentially I've got a grid of faces, and when you click on a face they turn your respective color (red and blue). You and your opponent take turns clicking on faces to color them your respective color. When any amount of faces are surrounded by faces that are of all the same color, then all the faces that are surrounded are deleted from the board, and the number deleted is added as score to the player whose color surrounded. And if you tap a face twice with the same color (so red face is tapped by red again) then it bursts leaving residue around that face's surrounding faces making it so those residued faces can't have their color change to the other color that's not the residue color.Now my hope is that I could get a slightly working AI, it doesn't have to be amazing or anything, just good enough to make decently intelligent moves and could possibly win. After doing some research it seems that using a MiniMax algorithm would be my best bet, but I have no clue how to create such a thing in unity. I was hoping someone might have some insight on how to accomplish this, or does anyone have a better idea of an algorithm that would be better in determining moves?
Thanks for the help!
This is horribly broad question, however - one of the most succesful approaches to the board games AI (especially GO) is UCT based approach. It is a monte carlo driven heuristic approximation of the minmax algorithm. MinMax requires game's state space to be very small in order to fit in both memory and time constraints. UCT on the other hand can make reasonable moves in any given amount of time (it is fully iterative).

Path finding in a highly dynamic world

I am working on a simple soccer simulation, I am using potential fields for collision avoidance more specifically following technique,
http://www.ibm.com/developerworks/java/library/j-antigrav/
only obstacles on the field are other players and they are constantly moving. Problem is it works if I assign really big push force to the characters since characters move at speed it takes some time to change direction but this has few drawbacks with such a high gravity I can never position an npc to grab the ball cause there is always some force pushing me around.
I though I could solve this by assigning pulling force to the ball but that actually made it worse. nps would go to the ball, ball starts pulling which makes npc push the ball it goes into a loop until npc crashes to a wall.
How I've implemented this is, I have a vector that would steer me towards my target then I add to that all gravitational forces acting on the npc and steer towards that direction.
Basically I am wondering what kind of improvements I can make? my current problem is not hitting other players precisely getting behind a ball without getting affected by other players.
I'm not sure that employing potential fields is the way to go here. If all players are directly between you and the ball, how do you get to it?
I'd be tempted to plot a straight line, then iteratively adjust the route for the position of other players, adjusting for their trajectories and — if you're really clever — anticipating changes in the same.
You could put some kind of limit as to the area of effect that another player's gravity well will have. Limit it to a small radius around that player so that when you're clear of other players, there are no forces acting on your character. You could also diminish the collision avoidance force when you're near the ball, reasoning that players care less about not hitting each other when it's time to kick the ball.

WPF tabswitch/ render takes too much time

I have a WPF application with many tabs..
in one tab.. i make a verycomplex vector drawing consisting of thousands of drawing visuals.. (this represents a machine and all elements need to be interactable..)
It takes 3/4 seconds for drawing this for the first time..After the first draw it should be done..
The problem is if i switch to another tab and comeback, it takes atlease 2,3 seconds to show the tabpage with drawing again.. Since there is no redraw, why should it take so much time..?
If the component is not going to change, you could call Freeze() on it to mark it as done. Without trying it out I don't know if that would help, but you could give it a shot.
Not all objects are Freezable. Check out the MSDN documentation for more info:
http://msdn.microsoft.com/en-us/library/ms750509.aspx
Another thing you could try would be rendering the vector art to a bitmap, and displaying that. Maybe it makes you feel icky to lose the vector precision, but if you know it's not going to change and it will look the same, what's the harm? (If you support printing or something that will require a hi-res version, you could always switch back for that operation.) For info on how to convert a UIElement to a bitmap, check out:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
Another possible solution: You don't really explain what kind of interaction you are doing with the elements, but if all you want to do is zoom and pan, a RenderTransform may be good enough (which is more efficient than a LayoutTransform and/or moving all the elements individually). I haven't played around with combining Freeze() and a RenderTransform, but you may be able to get the desired zooming while reducing the amount of layout WPF has to do.

How can I manipulate a RagDoll made with farseer physics in Silverlight?

I made a ragdoll similar to the one in this demo. This rag doll will be used for a turn based rpg game where the physics will be used for animations such as character taking damage, dying, falling down, etc.
What I am pondering at the moment is as how should go about this, should I stick the rag doll by the head to the background (leaving the body dangling) and basically throw around its body parts around as to simulate punching etc (as shown in Fig 1), or stiffen the joints and statically rotate and move the body parts for the actions taken(as shown in Fig 2), and when it comes to the character dying(or a similar action) just loosen the joints and let the rag doll fall down. Or is there a better way to go about doing this?
I am new to farseer physics and don't even know if what I mentioned is even possible or overwhelmingly hard to do.
Illustration http://img3.imageshack.us/img3/8681/charactermovementrg5.jpg
Please note that the red line in the figures represents the character's arm
Not sure that ragdolls are the way to go here, if you want animations. But if you do want to use them, I'd lock the feet to the floor and have some rotation springs in the joints so that when no forces are applied, the body stands upright. Then if it gets a hit, it'll kind of bend over, but should rebound to it's stand-up state afterwards (you may have to help it along the way back, e.g. apply some forces/torques until it's back where you want it).
For animations, such as the character punching, you could perhaps apply a spring joint (I think that's the name). Connect it to the fist and the destination, and the arm should automatically move there. You could do the same with a kick, just release the lock on that foot. However, I think it might be hard to get it to look right. On the other hand, it would look unique to other games, even though it might look kinda funny.
If you're skilled, you might wanna create an animation editor and save an animation as a sequence of forces and torques that need to be applied to limbs in order to get them to where and when they should be.
I think a better approach is to have an animated sprite played, rather than going through joint manipulation .
Maybe you can use some RotateTransform implementations to articulate arms and legs.
For sure animated sprites are the best, and painless, way of doing this.

Resources