Collision Detection in java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have some questions on collision detection. I am using LWJGL for my game in java. I have no experience in this subject and have been following the thin matrix OpenGL tutorials for the core of the game. I have experience in html, css, c++ and, of course, java.
I have been searching all over the web and have not found anything useful.
What is the simplest way to implement 3D collision detection in my games?

I would suggest using a 3d engine that recognizes collision for you like jMonkey. jMonkey can use LWJGL. There is an awesome tutorial here : http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_collision
If you want to roll your own like I do, I create a hash of the 3d points in space. If another object has the same hash it has near collision.
import static java.lang.System.out;
public class App {
public static int hash(float x, float y, float z) {
double hash = (((x * 31) + (y * 37) + (z * 41)));//31,37 and 41 are prime numbers
return (int) (hash);//multiplying the hash by 1000 for example may increase the accuracy significantly. Depends on the size of your space
}
public static void main(String[] args) {
for (float count = 0; count < 2; count += 0.01) {
out.println(hash(0, 0, count));
}
}
}
As you can see the hashes in the output show close collision as count increments by 0.01.
0
0
0
1
1
2
2
2
3
3
4
4
4
5
5
6
6
6
You can modify the prime numbers for higher accuracy.

Related

Calculate logarithm in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would I calculate logarithm in Java? How would I extend it to calculate for a sequence? I am trying to find solution for following:-
N different apps have different user growth rates. At a given time t, measured in days, the number of users using an app is g^t .
After how many full days will we have 1 billion total users across the N apps?
Example 1
Input: N = 1, growthRates = [1.5]
output = 52
Example 2
Input: N = 3, growthRates = [1.1, 1.2, 1.3]
output = 79
Logarithm in Java
To calculate a common logarithm in Java we can simply use the Math.log10() method:
System.out.println(Math.log10(100));
To calculate a natural logarithm in Java we use the Math.log() method:
System.out.println(Math.log(10));
To calculate a logarithm with custom base in Java (logb(n) = loge(n) / loge), we can do it by defining a method as below:
private static double customLog(double base, double logNumber) {
return Math.log(logNumber) / Math.log(base);
}
a details about mathematic and Logarithm can be found here

What the function rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE))); do in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Hello I an not a java programmer,
I need know what this function does:
rpcToken = Math.round(1E8 * Double.parseDouble("0." + (int) (Math.random() * Integer.MAX_VALUE)));
This just generate a random 8 number string? I need to make this exactly in PHP.
Thank you
Math.random() gives you a pseudorandom number less than 1 but at least 0.
This is multiplied by Integer.MAX_VALUE, which is 2147483647. The result is a number between 0 and 2147483646.
(int) converts this number to an integer.
"0." + converts this to a String, and adds "0." to the beginning of it.
Double.parseDouble converts this String back to a double. So now we again have a number between 0 and 1. But any number greater than 0 but less than 0.1 is impossible due to the way we created the String. "0.9" and "0.900" would both convert to 0.9.
1E8 * multiplies this by 1E8, which is 100000000.
And finally, Math.round rounds this off to the nearest integer.
So, in total, we have a supposedly random number somewhere between 0 and 99999999, but the final result is very skewed. Many numbers are impossible to generate with this code (numbers between 1 and 9999999 seem impossible, but 0 is still possible) and this code will generate certain numbers more often than other numbers (close to half the generated numbers will begin with 1, and more than average may end with 0), and I find it very unlikely that all of this is intentional (although it's impossible to say for sure without knowing the code's purpose.)
If you just need a random 8 digit number, there are easier, faster, and better ways. Why not just multiply that random number you got in the first step by 100000000, and round it off?

minimize absolute function in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm looking into absolute function minimization problem in Java. Can anyone suggest libraries and solutions?
I downloaded apache common math3, however, could not find related functions to create linear function to minimize absolute functions.
Simply, I am looking into writing below functions in Java.
f(x) = b + | x - a | + d + | x - c | + f + | x - e |
I've edited this problem a bit for my second problem, here b, d, f are also linear functions.
b = a1x + b1
d = a2x + b2
f = a3x + b3
If you are summing N terms of the form |x - a_i|, consider the gradient as x increases from negative infinity:
At negative infinity, and for most of the left-hand number line, the gradient is -N;
As you pass the minimum of the a_i values, the gradient increases slightly, to -N+2;
As you pass the next smallest of the a_i values, the gradient increases again, to -N+4.
Each further a_i passed, the gradient increases by 2;
At positive infinity, the gradient is +N.
So, the gradient starts negative, increases in steps at each of the a_i positions, and ends up positive; you're looking for the point or range where the gradient is zero. This will occur "in the middle", i.e. at the median of the values a_i.
If there are an odd number of points, the median will be at a point. This is the case in the question: the minimum is at median(a, c, e).
If there are an even number of points, the median is between two points. In this case, the function is minimised anywhere between those points.

Number of different possible solution to an equation in java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I need to find the number of total possible different solution to an equation with more than one variable.
For example:
1x + 2y + 8z = 13
What is the total number of different combinations for the values of x, y and z?
I can't think of an algorithm to solve this. I don't need the answers printed, just the total number of different combinations. The coefficients and variables will always be positive, and the final number too.
1x + 2y + 8z = 13
Hence try (x, y,z) from (0, 0, 0) upto (13, 6, 1)
Hence at most 14*7*2 tries
Sort by the highest coefficient: z, y, x. The last variable can be inferred

Java declaring variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I just started learning java and came across something that I havent seen before in one of the examples I was looking at, can anyone tell me how is this int variable defined? and is this used only in java or can be used in other languages? a link or document explaining it would be nice too, thanks in advance
int a = 10;
int b = (a >> 24) & 255;
b is defined to b the result of the bit operations (a >> 24) & 255
you can read about this operations here
This is shift operator in java
int a = 10;
it it represented in binary 1010
int b = (a >> 1);
This means 1010 one bit shifted and new binary will be 0101 and it is 5 in decimal

Categories

Resources