I am trying to run an evolutionary algorithm using xna
i would like only to run the logical side of the game
and after a long caculation time add the animation.
Does anybody know how to accelerate calculation time and disable the
Draw() call.
Thanks
Just don't draw in your Draw method. You can keep a counter or a time stamp as reference, and only draw once out of 100 times or once per second.
However, your problem is not the number of times Draw is called, but the number of times Update is called. By default, XNA will never call Update more than 30/60 times per second. You can change the frame rate XNA tries to achieve as explained in this post. To call Update 100 times per second, just change the target elapsed time in your game to:
this.TargetElapsedTime = TimeSpan.FromSeconds(1.0f / 100.0f);
Related
I'm using the Geolocator class from WP8 to built a small app that displays the current speed. I have set the Accuracy to High and MovementThreshold to 0.1 meters. At the moment, I'm using the PositionChanged event of the geolocator to get the speed while moving.
The problem is, that when I stop moving, there is no PositionChanged event fired, and thus the speed is most of the time not 0 when it should be 0. Sometimes it works, sometimes not, I guess it has something to do with stopping somewhere in between the threshold.
Another posibility I tried, is not using PositionChanged and polling the current position with GetGeopositionAsync every second using a timer to update the speed. This seems to work better but I guess that this is the more power consumptional method as I am getting the position no matter if it's neccessary or not.
So what is the best way to get the current speed, no matter if moving or not? Can it even be done with PositionChanged?
I guess there is no ideal solution for you.
In your case, I would go with a DispatcherTimer that would fire every second (for example).
You could use a timer as you mentioned, but instead of calling GetGeopositionAsync everytime, you can use the Geolocator instance to get current speed.
I guess speed doesn't only update when PositionChanged is fired so, that way, you could get an updated speed without consuming resources with GetGeopositionAsync.
I have a timer. When it ticks , by calculating based on formulas, the position of 12 panels changes.
The problem is, although timer's interval is 1 milisecond, the moves are very slow.There are many calculations. What can be done for improving speed, using drawing class or something else?
The gui shows positions, I can move the panels by clicking, so the values. If the correct way is drawing class, do I have a chance to move the rectangles by clicking and take the values of them?
although timer's interval is 1 milisecond
That's the core problem, a Timer cannot tick that fast. Actual timer resolution is constrained by the operating system's clock interrupt rate. Which ticks 64 times per second on most Windows machines. Or once every 15.625 millsecond. The smallest interval you can hope to get is therefore 16 msec. So these panels probably now move 16 times slower than you hoped they would.
Keep in mind how this is observed, you only need to keep human eyes happy. They can't perceive anything that changes at 1 msec, anything that updates faster than 25 times per second just looks like a blur. Something that's taken advantage of in TV and the cinema, a movie updates at 24 frames per second. Once ever 42 milliseconds.
So a sane setting for the Timer.Interval is a hair below three times the clock interrupt rate, 46 milliseconds. The actual tick interval will be 3 x 15.625 = 46.875 msec on a regular machine. And still close to 46 msec if the machine runs with a higher clock interrupt rate. You'll get an equivalent frame rate of 21 fps. Right on the edge of a blur to human eyes. The next lower sane rate is two times the interrupt rate or 31 msec for 32 fps. Making it any smaller doesn't make sense, it isn't observable and just burns cpu time for no benefit.
And, important, the rate at which the panel moves is now determined by how much you change its Location property in the Tick event handler. The interval is fixed so the amount of motion you get is determined by the increment in the position. Which will not be one pixel, probably what you are using now.
I am making the character run but the animation is extremely quick as I am doing:
_frameIndex++;
_frameIndex; is the value which points to the image in the SpriteSheet. Does anyone know how I can use gameTime.ElapsedGameTime.TotalMilliseconds to slow the animation down?
I saw that you've asked a couple of questions tonight concerning animation and spritesheets, so here's an example from Aaron Reed's "Learning XNA 4.0," from Chapter 3 under the "Adjusting the Animation Speed" heading.
First, create two class-level variables to track time between animation frames:
int timeSinceLastFrame = 0;
int millisecondsPerFrame = 50;
The first variable tracks time passed since the animation frame was changed, and the second is an arbitrary amount of time that you specify to wait before moving the frame index again. So making millisecondsPerFrame smaller will increase the animation speed, and making it larger will decrease the animation speed.
Now, in your update method, you can take advantage of game.ElapsedGameTime to check time passed since the last frame change, and change the frame when when that value greater than millisecondsPerFrame, you can do work:
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame){
timeSinceLastFrame -= millisecondsPerFrame;
// Increment Current Frame here (See link for implementation)
}
This sort of solution is similar to what you've found works, except that you can take an extra step to specify exactly how often you want the animation to update, or even change that duration later on in your code if you like. For example, if have some condition that would "speed up" the sprite (like a power-up) or likewise slow it down, you could do so by changing millisecondsPerFrame.
I removed the code that actually updates the current frame, since you should already have something that does that since you have a working animation. If you'd like to see the example in-full, you can download it from the textbook's website.
I debugged the code and noticed that the gameTime.ElapsedGameTime.TotalMilliseconds always equated to 33. So I did the following:
milliSeconds += gameTime.ElapsedGameTime.Milliseconds;
if (milliSeconds > 99)
{
_frameIndex++;
milliSeconds = 0;
}
Which basically means that if this is the THIRD frame of the game, then make he _frameIndex go up. Reset the milliseconds to start over.
I want to perform a discrete-event simulation in C#. I want three balls rolling on the screen simultaneously following a random walk pattern. At time 1 ball one should appear and start rolling, at time 5, ball 2 and at time 10, ball 3 should appear. When any two balls come enough closer the color of balls should change (as long as they stay close).
I am very new to discrete event simulation, and i want to understand, how we do it in C# programming? what steps are required in creating the model. I know graphics and other stuff.
New comers be advised:
Using operating system timers or threads is NOT the way the discrete event simulations should work. Using one of these as a building block might be misleading or a plain wrong.
Read the wikipedia article first about Discrete Event Simulation (DES)
There are" models" so called "formalisms" that mathematically proven to work in event simulation. You need to implement one (for example DEVS).
You may want to look at some listed DES List of discrete event simulation software at the wikipedia.
Also you may find useful sigmawiki (programs,examples, tutorials) about DES. SharpSim and React.NET are DES implementations in C#.
Use a Timer (drag one from the Toolbox over to your form in the designer, or instantiate it in code if you prefer). Double click the timer to set a _Tick event in your code which will fire every N milliseconds (the .Interval property of the timer governs this). Set the .Interval to 1000 (1 second), and use objects that keep track of their own position in X and Y coordinates.
Use a Random object to generate the direction of the next position change of the ball, and within the _Tick event of the timer, update the position variables for each of the balls.
Using raw threads is a possibility, too, but the Timer gives you some of that power without having to manage everything yourself.
I'm trying to get a general smooth scrolling mechanism that I can implement in my mobile applications.
I want it to be generic enough so that it can port to any platform, but I am currently working in C# on the .net Compact Framework.
What I'm doing right now is:
Create a Stopwatch object (in the panel's ctor)
on mouse down start the Stopwatch and save the current mouse point _lastMouse
on mouse move, stop the Stopwatch, and store velocity = (_lastMouse - curMouse) / Stopwatch.TotalSeconds then reset the Stopwatch and start it again
In most cases Stopwatch.TotalSeconds is between 0.02 and 0.03
on mouse up, I pass the velocity value into a smooth scrolling function, and that function continues to scroll the panel until either the end is hit or the increasing friction causes the velocity to be == 0
My problem is in the final step. The velocity values are generally int the 2,000-3,000 pixel range. The velocity is in pixels per second, so this is to be expected. I take the Stopwatch (which should be still running), stop it and I find the elapsed time from last mous move and multiply velocity by Stopwatch.TotalSeconds, get that distance and then reset and start the Stopwatch, then loop back and start all over again.
The expected result is that the elapsed time between refreshes multiplied by the velocity should give me the number of pixels (according to the last mouse move) that I should scroll. My actual result is that sometimes the panel goes flying and sometimes it bearly moves! the gradual slowdown is fine, it's just the beginning velocity that is off
Is there a flaw in the logic? Should I be doing something else?
Thanks for any help!
It seems to me that there are three possible sources of inaccuracy here. Firstly, as "A.R." said, you'll have problems if the granularity of your timer isn't good enough. Have you checked IsHighResolution and Frequency to make sure it's OK?
Secondly, even if your timer is perfect, there may be some inaccuracy in your position measurements, and if you're taking two in very quick succession then it may hurt you. My guess is that this isn't a big deal, but e.g. if you're on a capacitive touchscreen then as the finger lifts off you may get variation in position as the contact area goes down.
Thirdly, the physical motion of the finger (or stylus or mouse or whatever you've got doing the actual input; I'm going to guess a finger) may not be all that well behaved. At the end of a gesture, it may change from being mostly horizontal to being mostly vertical.
All these problems would be substantially mitigated by using a longer sampling period and maybe (try it both ways) ignoring the very last sample or two. So, keep a little circular buffer of recent samples, and when you get the mouse-up look back (say) 100ms or thereabouts and use that to decide your velocity. Or, if you don't want to keep that much history, use a simple IIR filter: every time you get a sample, do something like
filtered_dt = filtered_dt + SMALL*(latest_dt-filtered_dt);
filtered_dx = filtered_dx + SMALL*(latest_dx-filtered_dx);
filtered_dy = filtered_dy + SMALL*(latest_dy-filtered_dy);
where SMALL should be, at a guess, somewhere around 0.2 or so; then use filtered_dx/filtered_dt and filtered_dy/filtered_dt as your velocity estimate. (I think this is better than calculating a velocity every time and filtering that, because e.g. the latter will still blow up if you ever get a spuriously small dt. If in doubt, try both ways.)
If you use the IIR approach, you may still want to make it ignore the very last sample if that turns out to be unreliable; if you remember the latest dt,dx and dy you can do that by undoing the last update: use (filtered_dt-SMALL*latest_dt)/(1-SMALL), etc.
Here's an off-the-wall suggestion that may or may not work. You mentioned that you get more erratic results when there's a "flick" at the end of the gesture. Perhaps you can use that to your advantage: look at, say, how rapidly the estimated velocity is changing right at the end of the gesture, and if it's changing very rapidly then increase the velocity you use somewhat.