Monday, October 24, 2011
Thursday, September 29, 2011
[Java] Generate random numbers in a range
I knew the method I've been using to generate random numbers in a range was very inefficient as it would be doing unnecessary calculations an extra several hundred times per second; causing the UI of my games to become choppy. The only problem was, I wasn't sure of a better way to do it...
I was doing something along the lines of:
int low;
int high;
int temp = random(high);
while (temp < low) temp = random(high);
return temp;
Yes, that did get the job done, but as I mentioned, it was doing WAY more calculations than it should have been.
There must be a simpler way...
Then it hit me...
public int getRandom(int low, int high){
return (int)(low + (random.nextInt(high - low)));
}
In 1 call, as opposed to the many that could potentially have been done before, the above code produces a number within range.
It creates a random number based on the difference between the high/low, and then adds the random value to the minimum in the range.
So far this is the most efficient way I've found to generate random numbers in a range and has saved me a lot of unnecessary computations; making the games look much crisper.
Thank you math.
I was doing something along the lines of:
int low;
int high;
int temp = random(high);
while (temp < low) temp = random(high);
return temp;
Yes, that did get the job done, but as I mentioned, it was doing WAY more calculations than it should have been.
There must be a simpler way...
Then it hit me...
public int getRandom(int low, int high){
return (int)(low + (random.nextInt(high - low)));
}
In 1 call, as opposed to the many that could potentially have been done before, the above code produces a number within range.
It creates a random number based on the difference between the high/low, and then adds the random value to the minimum in the range.
So far this is the most efficient way I've found to generate random numbers in a range and has saved me a lot of unnecessary computations; making the games look much crisper.
Thank you math.
Wednesday, September 28, 2011
Clickbank issues accepting payments?
Earlier today a customer brought it to my attention that they were unable to purchase The Guardian Eye from my servers.
I was told he received an error, "Unable to process your order at this time."At first, I thought it was an error on his part, as I checked the page and it seemed as if it was working properly. Then he told me he tried with 4 different web browser, used his credit card on a different website to check it, and still ran into the same problem.
So, I did a little investigation and came across this post: http://forums.digitalpoint.com/showthread.php?t=931924
I had always been curious as to why one day I would sell hundreds, and the next, nothing (which with constant traffic is a statistic anomaly, even more so that it happens quite often). This sparked my interest, so, I investigated further by checking my own Clickbank analytics.
What I found is fairly aggravating: There were far more order form impression (OFI) than there actually were sales (An OFI is logged when a user clicks a Buy Now link and they're bought to the actual payment page).
Now that's just plain wrong.
If there's an OFI, the probability that the user had the intention of purchasing the software is much higher than if they had just visited the sales page. Granted, a loss can still be expected as nothing is 100%, but the likelihood of users leaving the order page is much lower than the likelihood of a user leaving the pitch page.
So what's going on with Clickbank?
How can such a large company be failing so miserably?
I honestly think it may be time to find a new affiliate marketing company to sell my products, as I am definitely not liking the profit loss I'm currently experiencing.
Oh yeah, I forgot... There aren't really any other digital product affiliate marketing site...
It may be time to make one.
[Java] Circle to Circle Object Collision Detection
With any video game, it can be necessary to detect whether or not two objects are touching.
Touching, in this sense, means that one object's (or image's) pixel position is over the pixel position of another.
For the sake of this post, I'm sticking strictly to circle-to-circle collision detection, as it was something I had to figure out, which can be easily related to other types of collision.
I tried many different approaches by comparing various x/y components, and finally settled on this much simpler solution:
public boolean checkCollision(Circle a, Circle b){
//Circle has 3 components: x, y, and radius
//We're going to use the distance formula here
//We'll start be getting the x and y differences between the objects's centers
int xDif = Math.abs(a.x - b.x);
int yDif = Math.abs(a.y - b.y);
//Now to get the distance, we raise both differences to the power of 2
//Then we add them together, and take the square root
int distance = Math.sqrt(Math.pow(xDif, 2) + Math.pow(yDif, 2));
//If the calculated distance is less than the combined radii then the circles are overlapping
if ((a.radius + b.radius) > distance) return true;
else return false;
}
Boom. Basic circle to circle collision detection.
Touching, in this sense, means that one object's (or image's) pixel position is over the pixel position of another.
For the sake of this post, I'm sticking strictly to circle-to-circle collision detection, as it was something I had to figure out, which can be easily related to other types of collision.
I tried many different approaches by comparing various x/y components, and finally settled on this much simpler solution:
public boolean checkCollision(Circle a, Circle b){
//Circle has 3 components: x, y, and radius
//We're going to use the distance formula here
//We'll start be getting the x and y differences between the objects's centers
int xDif = Math.abs(a.x - b.x);
int yDif = Math.abs(a.y - b.y);
//Now to get the distance, we raise both differences to the power of 2
//Then we add them together, and take the square root
int distance = Math.sqrt(Math.pow(xDif, 2) + Math.pow(yDif, 2));
//If the calculated distance is less than the combined radii then the circles are overlapping
if ((a.radius + b.radius) > distance) return true;
else return false;
}
Boom. Basic circle to circle collision detection.
Subscribe to:
Posts (Atom)