I want to know the difference between cvWaitKey(0) and cvWaitKey(10) - java

cvShowImage("SMART", cropped);
cvWaitKey(10);
cvShowImage("SMART", cropped);
cvWaitKey(0);
What is the difference between these two functions and when i use this in infinite for loop cvWaitKey(10) works but cvWaitKey(0) or cvWaitKey(30) do not work. What is the reason?

The difference can be found in the documentation of OpenCV of the waitKey function.
Basically, the function waitKey waits for a key to be pressed, and the argument is the amount of time it will wait. So, when you use 10. It will wait 10 milliseconds and then continue with the program.
The documentation says:
0 is the special value that means “forever”
So, when you use 0. The program will wait for key to be pressed forever... Just pressing any key will continue the program... (also closing the window will do it)
I recommend to use 10 when you are doing a stream of pictures (maybe from a camera). And use 0, when you are expecting human interaction for the program to continue. And a bigger value if you want to see it for enough time, but continue the program without any interaction.

Related

AnyLogic : All the delays are full So new agent is not suppose to enter according to condition

Of the conditions I listed in SelectOutput8, one of them must be true every time.
But that is not the case, and it seems like there is no reason for that.
Details are shown in the image below.
The problem is the delay2, delay4, delay7 is full So no new agent needs to enter according to the condition but that is not the case.
First i have to say that those seize/release blocks are completely useless and probably are just a desperate way you are using to solve the problem
Now, according to your model, there is absolutely nothing blocking your model to move an agent to your seize block because the condition clearly states that if delay1 is free, the agent can exit delay (and in your picture, delay1.size IS equal to zero)
I don't know what you have in selectOutput7 though
Instead of taking select block, you can try with multiple split block. Split blocks allow you to change or create new agent.

Slow program and reducing performance over time

My program is a Java game which involves taking turns between the user and AI. Therefore after all operations are complete I have a infinite while loop which only breaks after the turn has changed. I only use an infinite loop because I am using a timer and cannot predict when the user ends their turn. But I notice that my program slows down over time to a point where even clicking buttons has no effect. Is it my loop which is causing this? Help would be appreciated.
while(true) {
if(playerTurn % 2 == 1) {
artificialIntelligence();
break;
}
}
If you use an infinite loop(while loop in your case) this operation would be performed continuously; thus slowing your application. Hence, I would suggest breaking the code into two threads.
First thread - Check user-turn event.
Second thread - Do the AI stuff.
As soon as the user event occurs, stop the thread and do what's needed.
This way your code would never be blocked at any point of time; thus resulting in better performance.
Without more code, it is hard to determine what is the real cause of the problem. However, one suspect may be that you're holding on to object references and they're not being reclaimed by garbage collection. Try using a java profiler, it can help you to pin point where exactly the issue may arise.

Making Java game, found easy way to cheat, don't know how to prevent it

I'm making a game in Java, and I made it so that if you right click, the player teleports to the mouse to "escape". I want to make it so that you can only use it every 2 mins. and after trying and failing THAT, I found out that you can just hold down right mouse and the player will follow your mouse/clicker. I am using Processing 3.1.2 if that helps at all.
Every time you allow that player power to be used, note the current timestamp.
Next time the player attempts to activate that power, check the saved timestamp against the current time. If an insufficient number of seconds have passed, disallow the power.
If sufficient time has passed and you allow the power to activate, update the variable holding the time that the power was last used.
This is often called a "cool down" in games.
I would suggest using a javax.swing.timer. I have done this before, and within the mouseClicked event you set a boolean canTeleport = false. At the end of the javax.swing.timer, set canTeleport = true. The first thing that you can do when going inside mouseClicked,
if(canTeleport)
{
//teleport
}
//start timer

Is it possible to control polling intervals with GLFW?

I have a slight stutter in my 3D openGL application which I think can be resolved by reducing the amount of keyboard input that is processed. So, I was wondering if it is possible to set an interval on glfwPollEvents or somehow control when/how often glfwSetKeyCallback accepts key input? (eg. user holds the key down but I only want to process it every x millisecs). I have tried using a Timer object but that results in an illegalmonitorstate exception.

Python - Threading, Timing, or function use?

I am having an issue formulating an idea on how to work this problem. Please help.
My project consists of an N x N grid with a series of blocks that are supposed to move in a random direction and random velocity within this grid (every .1 seconds, the location of the block is updated with the velocity). I have three "special" blocks that are expected to have individual movement functions. I will have other blocks (many of them) doing nothing but updating their location, and making sure they remain in the grid.
Now these three blocks have functions beyond movement, but each of these runs individually, waiting for the other block's special function to finish (block 2 will wait on block 1, Block 3 will wait on 2 and set it back to block 1, etc.) This queue of sorts will be running while the motion is happening. I want the motion to never stop. After each block's non-movement function runs n times, the code finishes.
My question is this: should I use threads to start and stop the non-movement functions, or is there a way to just set a time and set booleans that could use a class function after .1 seconds to continuously move the objects (and obviously loop over and over), and then use counts to end the program all together? If so, how would you write main function for this in Python? For all of this happening, does anyone think that Java would be significantly faster than Python in running this, especially if writing the data to a .txt file?
Your best bet is probably to handle all of them at once in a single update function rather than attempting to use Threads. This is primarily because the Global Interpreter Lock will prevent multiple threads from processing concurrently anyway. What you're after then is something like this:
def tick():
for box in randomBoxes:
box.relocate()
specialBlock1.relocate()
specialBlock2.relocate()
specialBlock3.relocate()
Then we define a second function that will run our first function indefinitely:
def worker():
while True:
tick()
sleep(0.1)
Now that we have an interval or sorts, we'll launch a Thread that runs in the background and handles our display updates.
from threading import Thread
t = Thread(target = worker, name = "Grid Worker")
t.daemon = True # Useful when this thread is not the main thread.
t.start()
In our tick() function we've worked in the requirements that specialBlocks 1, 2, and 3 are working in a set order. The other boxes each take their actions regardless of what the others do.
If you put the calls to the special functions together in a single function, you get coordination (2) for free.
def run(n, blocks):
for i in range(n):
for b in blocks:
b.special()
As for the speed of Python versus Java, it depends on many things, such as the exact implementation. There is too little information to say.

Categories

Resources