I'm trying to take an OpenGL mesh that I have and give it a random position within a room. I currently have this line of code:
positions[i] = glm::vec3(rand() % 10 - 1, rand() % 10 - 1, rand() % 10 - 1);
Which just generates a random position (x, y and z). However I'm not entirely sure what the '% 10 - 1' is doing since this code snippet was used online. I can alter the numbers and the mesh moves in the scene but I'm not entirely sure how the numbers are affecting it.
How can I edit the line so that the meshes are not too close together and all fit within the room (I know you don't know the room's dimensions/position, so just an explanation would be very helpful).
This part rand() % 10 - 1 : rand() is random number generator. % is modulo operation. So basically rand() % 10 generates all the numbers in the range [0,9]. Now the rand() % 10 - 1 will change the range to [-1,8].
How can I edit the line so that the meshes are not too close together and all fit within the room (I know you don't know the room's dimensions/position, so just an explanation would be very helpful).
Simply change 10 to your dimensions max value.
Related
Use case: Live images are provided in real time and are to be viewed on the screen. To lower the CPU load, the user should be able to discard images and only show, say 10%, or 50%, of the images.
If the user choose 50%, then every other image should be shown (not 50 images in a row, and then discard 50, as that would be 50% too..)
The current code:
void paintImage(int everyWhatImage)
{
showImage();
}
shows the image 100% of the times.
If the user supply an integer, like 1,2,3 .., meaning every one, every second, every third and so on, something familiar like this could be used:
void paintImage(int everyWhatImage)
{
if(counter % everyWhatImage)
{
showImage();
}
counter++;
}
However, the above algorithm don't support showing less than 50% (the '2') of the images, and so the question is, how to do that?
As this is a live streaming application, it needs to be fast.
In addition, the above code is executed in a callback function, so there is no knowledge of when it is to be executed.
Any given picture is either shown, or not shown. If it is not shown, then the chance of any future picture being shown should go up. If it IS shown, the chance of any future picture should go way down.
So, count up to 100. If you get on or over 100, show the picture... and subtract 100:
static counter(0);
if (counter >= 100) {
showImage();
counter -= 100;
}
counter += chance;
where chance is for example 70, which would mean 'show 70% of the pictures'. Going through it:
first picture: Not shown; counter is at 70.
second picture: Shown; counter is at 40.
third picture: Shown; counter is at 10.
fourth picture: Not shown; counter is at 80.
fifth picture: Shown; counter is at 50.
sixth picture: Shown; counter is at 20.
seventh picture: Not shown; counter is at 90.
eighth picture: Shown; counter is at 60.
ninth picture: Shown; counter is at 30.
tenth picture: Shown; counter is at 0.
.. and it loops from there.
Save up the given proportion in a "running balance". Every time it reaches at least 1.00, "cash in" the savings for an image.
Choosing 27% for an example ...
show_count = 0.00
ratio = 0.27
while (we have more images to show) {
show_count += ratio
if (show_count >= 1.00) {
show_image()
show_count -= 1.00
}
The other answers to this question all appear to implement a counter, so I thought I would suggest a probabilistic approach. You could generate a random number between zero and one each time your function is called:
random = ((double) rand() / (RAND_MAX))
Then, if the random number is below the prescribed percentage of images to be shown, show the image. Otherwise, discard it.
I have a task which is creating an Dungeon Escape game, based on a 4x4 array. I first have to create 3 different postion for E(Exit),K(Key) and P(Player). How can I random those 3 in 3 different position in an array ?
EDIT: I meant I need to find a way to random 3 DIFFERENT POSITION the neatest way. I already know about the rand() and srand thing.
Try to use rand function which is used to generate random number.
First of all you need to initialize random seed with srand(time(NULL)). myRandomNum = rand() % 4; generate number between 0 and 3.
So, you can generate two number which would represent row index and column index and repeat that three times for: E(exit), K(key) and P(Player).
According to the HOG process, as described in the paper Histogram of Oriented Gradients for Human Detection (see link below), the contrast normalization step is done after the binning and the weighted vote.
I don't understand something - If I already computed the cells' weighted gradients, how can the normalization of the image's contrast help me now?
As far as I understand, contrast normalization is done on the original image, whereas for computing the gradients, I already computed the X,Y derivatives of the ORIGINAL image. So, if I normalize the contrast and I want it to take effect, I should compute everything again.
Is there something I don't understand well?
Should I normalize the cells' values?
Is the normalization in HOG not about contrast anyway, but is about the histogram values (counts of cells in each bin)?
Link to the paper:
http://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf
The contrast normalization is achieved by normalization of each block's local histogram.
The whole HOG extraction process is well explained here: http://www.geocities.ws/talh_davidc/#cst_extract
When you normalize the block histogram, you actually normalize the contrast in this block, if your histogram really contains the sum of magnitudes for each direction.
The term "histogram" is confusing here, because you do not count how many pixels has direction k, but instead you sum the magnitudes of such pixels. Thus you can normalize the contrast after computing the block's vector, or even after you computed the whole vector, assuming that you know in which indices in the vector a block starts and a block ends.
The steps of the algorithm due to my understanding - worked for me with 95% success rate:
Define the following parameters (In this example, the parameters are like HOG for Human Detection paper):
A cell size in pixels (e.g. 6x6)
A block size in cells (e.g. 3x3 ==> Means that in pixels it is 18x18)
Block overlapping rate (e.g. 50% ==> Means that both block width and block height in pixels have to be even. It is satisfied in this example, because the cell width and cell height are even (6 pixels), making the block width and height also even)
Detection window size. The size must be dividable by a half of the block size without remainder (so it is possible to exactly place the blocks within with 50% overlapping). For example, the block width is 18 pixels, so the windows width must be a multiplication of 9 (e.g. 9, 18, 27, 36, ...). Same for the window height. In our example, the window width is 63 pixels, and the window height is 126 pixels.
Calculate gradient:
Compute the X difference using convolution with the vector [-1 0 1]
Compute the Y difference using convolution with the transpose of the above vector
Compute the gradient magnitude in each pixel using sqrt(diffX^2 + diffY^2)
Compute the gradient direction in each pixel using atan(diffY / diffX). Note that atan will return values between -90 and 90, while you will probably want the values between 0 and 180. So just flip all the negative values by adding to them +180 degrees. Note that in HOG for Human Detection, they use unsigned directions (between 0 and 180). If you want to use signed directions, you should make a little more effort: If diffX and diffY are positive, your atan value will be between 0 and 90 - leave it as is. If diffX and diffY are negative, again, you'll get the same range of possible values - here, add +180, so the direction is flipped to the other side. If diffX is positive and diffY is negative, you'll get values between -90 and 0 - leave them the same (You can add +360 if you want it positive). If diffY is positive and diffX is negative, you'll again get the same range, so add +180, to flip the direction to the other side.
"Bin" the directions. For example, 9 unsigned bins: 0-20, 20-40, ..., 160-180. You can easily achieve that by dividing each value by 20 and flooring the result. Your new binned directions will be between 0 and 8.
Do for each block separately, using copies of the original matrix (because some blocks are overlapping and we do not want to destroy their data):
Split to cells
For each cell, create a vector with 9 members (one for each bin). For each index in the bin, set the sum of all the magnitudes of all the pixels with that direction. We have totally 6x6 pixels in a cell. So for example, if 2 pixels have direction 0 while the magnitude of the first one is 0.231 and the magnitude of the second one is 0.13, you should write in index 0 in your vector the value 0.361 (= 0.231 + 0.13).
Concatenate all the vectors of all the cells in the block into a large vector. This vector size should of course be NUMBER_OF_BINS * NUMBER_OF_CELLS_IN_BLOCK. In our example, it is 9 * (3 * 3) = 81.
Now, normalize this vector. Use k = sqrt(v[0]^2 + v[1]^2 + ... + v[n]^2 + eps^2) (I used eps = 1). After you computed k, divide each value in the vector by k - thus your vector will be normalized.
Create final vector:
Concatenate all the vectors of all the blocks into 1 large vector. In my example, the size of this vector was 6318
I've spent many frustrating hours and cannot figure this out, i understand collision and have it working until i try to implement gravity, i cant seem to set the player position after it hits the tile map, falling through the ground is my problem, x axis a variation of the following code works fine
if (background.colMap[tiles[i].y][tiles[i].x] == 1)
{
playerSpeed.y = -0.f;
playerSprite.setPosition(playerSprite.getPosition().x, playerSprite.getPosition().y - 1);
inAir = false;
}
I though by reducing the speed to 0 and bumping the player back 1 pixel would work, but all it does is the player sprite bounces up and down
Given the above information, I assume you're making a side-scrolling game, and your character is colliding with the top of a tile.
That said, the first thing you need to understand is that you're not supposed to adjust the position of the character after it moved but before it moved. The character is never supposed to be in a position that is "illegal" in your game. Even for a fraction of a second.
You have the power to see the future (at least in your own game), use it at will! Always be one step ahead.
How to find the right place?
Basic algebra!
Here's the situation.
The goal here is to find where the green and red dotted line cross the small blue dotted line (which represent the ground).
First, we need to find the equation of our character (black dot) trajectory, which should looks like: y = ax + b.
Where the slope a = (Y2 - Y1)/(X2 - X1) and b = y - ax.
In our example case, the equation is y = -2x + 10. We just need to know what is the value of X when Y = 3, which we can find with x = (y - b) / a and in our case, x = (3 - 10) / (-2) = 3.5.
So we now know that our character will be intersecting with the floor at (3.5, 3) and that's where we will put the character.
Flaws of your current technique
When you collide, you put the character up 1 pixel if I understand correctly your code.
Imagine that your character is going really fast and in one update of his position, he gets from a valid position to an invalid one, 25 pixels below the ground. With your current technique, it will take at least 25 position updates to get back on the ground or maybe just 25 collision detection loops, but still, it's not very efficient.
Another thing, you seem to be looping every possible tiles in the level, so that's probably mostly empty tiles and/or full ground (inaccessible) tiles, which is a big overhead on what you really need.
The best option would be to store the coordinates of collidable tiles and just iterate those tiles.
If you have a screen of, let's say 50 x 50 tiles, and there are only 25 collidable tiles, you're still checking 50 * 50 - 25 = 2475 tiles, and these are 2475 unnecessary checks. But obviously, that's not the reason why you are having trouble, even those 2475 unnecessary checks won't break the logic.
And just to play with the numbers, since our character is 25 pixels below, we'll loop 25 times 2500 tiles, which is 62500 checks, instead of 25 * 25 = 625 with a collidable tile collection, or just 25 checks with the math stuff.
I have a c++ program which the user will click on two points on the screen and I have to create a logarithym scale from that.. Like:
10 100 1000 10000
given that my first point is at 10 supossed pixel 5 and 10000 is given at pixel 200
So how do I calculate the equation that would make my mouse show the log value when it points to the screen.
Thanks.
All you need is the log function. Let's first assume no offset. If you are given a value of x on the X-axis, you can get it's log value (e.g. in base 10) by:
log(x) / log(10)
If you want x to count from a certain offset (say x0), you should adjust x:
log(x - x0) / log(10)
If you want the resulting point to be offset at a point (say lx0), well just do it:
log(x - x0) / log(10) + lx0