I have an object with velocity of -5 going towards a wall. When it hits the wall, I want it to get a velocity of 5, thus reversing directions and rebounding. However, when I run it, it is not working. I've used several variations, I guess I am making a syntactical error.
public void action(int t) {
setVY(-5); //works - no surprises sets velocity to -5
.
.
if(getY() <= 0 ) {
setVY(5); //THIS METHOD DOESN'T WORK
setVX(5); // works no surprises , ball goes right
hooks.setMessage("hits wall", 25); //no surprises
}
}
This does compile and run with no errors. The object now goes north and then veers north/east at 45 degrees.
This works. Although it does perhaps not work as you expect. Are you remembering to add the velocity to the position, before checking the position again? If not, getY() will still return 5, and it will flip the sign of yv again (so it's back the way it was). You can write a small unit test which confirms that this works.
Since your example is not functional, it's hard to say exactly what's wrong, but if you feed the algorithm with the correct data, it will flip the sign of the y velocity.
i'm sorry I can't comment on your question therefor I use the answering facility. Could you be more specific as to what is not working. Is you value for vy wrong or is the setter setVY not setting the right value to your object? Or is something different not working?
as Gilbert above mentioned: Why do you compare y to 5? Couldn't there be situations where y never is exactly 5 something a little bit smaller or larger than 5. E.g. the object is already a small fraction "inside" the wall and you still want the object to change direction and rebound...
Bye,
Markus
Related
I'm new in programming and I'd like some help for an assignment, I only need a little clue to help me getting started (no answer, I only want to get a direction of how to do it then work my way).
The rules of the game : on the initial square is a marker that can move to other squares along the row. At each step in the game, you may move the marker the number of squares indicated by the integer in the square it currently occupies. The marker may move either left or right along the row but may not move past either end.
The goal of the game is to move the marker to the cave, the “0” at the far end of the row. In this configuration, you can solve the game by making the following set of moves:
eg: 2 3 1 2 4 0
first: move of 2 (to the right since it's impossible to go to the left) then: either move 1 to the right or 1 to the left then: if we moved left before: move 3 to the right (cannot go 3 to the left) if we moved right before then it's either 2 to the left or 2 to the right. 2 to the right is the right answer since then the next value is 0.
I must write the program that will try all the possibilities (so using a loop or a recursive I guess?) and return TRUE if there's a solution or FALSE if there are no solution. I had to choose the data structure from a few given by the prof for this assignment and decided to use an Array Based List since the get() is O(1) and it's mainly the only method used once the arraylist is created.
My program is only missing the (static?) method that will evaluate if it's possible or not, I dont need help for the rest of the assignment.
Assume that the ArrayBasedList is already given.
Thanks for your help!
I think your idea about using a recursive method makes a lot of sense. I appreciate you don't want the full answer, so this should just give you an idea:
Make a method (that passes in an integer x of the current position and your list) that returns a boolean.
In the method, check the value of list.get(x).
If it's 0, return true.
If it's -1, return false (more on that later).
Otherwise, store the value in a variable n, replace it with a -1, and return method(x+n) || method(x-n).
What the -1 does is it eventually fills everything in the array with a value that will terminate the program. Otherwise, you could end up with an infinite loop (like if you passed in [1, 2, 4, 2, 3]) with the value going back and forth.
A couple of things to keep in mind:
You'd need to make a new copy of the array every time, so you don't keep overwriting the original array.
You'd need to incorporate a try-catch (or equivalent logic) to make sure you are not trying to measure out of bounds.
If you have any further questions, feel free to ask!
When two of my objects collide, I want one of them to push the other.
while (checkCollision(this, cl)){
//cl is referring to the other entity, non-cl things my entity
double an = Assist.angle(new Vector3f(cl.z, 0, cl.x),
new Vector3f(z, 0, x));
double moveZ = Math.cos(Math.toRadians(an));
double moveX = Math.sin(Math.toRadians(an));
cl.z += moveZ;
cl.x += moveX;
}
The checkCollision() method works perfectly fine, detecting if my two objects (in this case 3D ships) have collided.
I am then getting the angle between the other object and my current one, and then using that angle, I am pushing away the other object. However - It isn't working.
The other object always seems to be pushed torwards the +x direction, and the Z doesn't seem to have any strict pattern, but isn't working correctly either.
Here is my method to calculate the angle (Assist.angle()):
public static double angle(Vector3f vec1, Vector3f vec2){
double so = Math.toDegrees(Math.atan2(((vec1.z) - vec2.z), ((vec1.x) - vec2.x)));
return Math.abs(so);
}
There must obviously be something blatantly wrong here - But I'm not seeing it, and I've been working for hours to try and get this code to work.
Math.atan2() takes vector cords not angles, radian or otherwise. What it returns is in radians.
For historical reasons to stupid to go into Math.atan2() expects y before x.
http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double)
Also I don't think that Math.abs(so) at the end is helping anything.
Take a look at this:
From your comments it sounds like you think you need Ab. You don't. If you just want to bounce off (which is a grand simplification) you need Ta and it's compliment.
I'm having the weirdest problem with my game in java. :|
I can get around it, so I will be able to continue on this game without a solution, but I just thought it was really weird.
It's a 2D Minecraft-ish (I know, I know, it's just for practice) game, and I'm trying to destroy blocks by clicking them, which is working, but not as expected.
mX = (x * blockWidth - screenWidth * blockWidth / 2 + blockWidth / 2 + e.getX()) / blockWidth;
mY = y - screenHeight / 2 + .5 + e.getY() / blockHeight;
These two lines should define the x and y coordinates of the block the mouse is currently hovering over. Filling in the same numbers will result in the same answer as the second formula is a shorter version of the first. However, the second one does not work, clicking a block in the upper half section will result in breaking the block above it. The only reason I can think of is that, in the mouseClicked() method, the (int) (which is required as you can't click on half coordinates) rounds the first one up and the second one down because of the .5 in it. I'm not sure, that's why I'm asking you. :)
Does anyone know what the cause of this might be? I prefer not to use the upper formula because I want it to be much simpler.
Java is doing some hidden casts. Depending on which variable is a double or an int, you might get unexpected results because some double values have been casted to ints. I would make sure all your variables are doubles.
Also, do not forget that floating points also have representation limitations, that can affect computation and computation results.
If you want to round your value, you can use:
Math.round(2.7);
if you want to "round up", you can use:
Math.ceil(2.7);
if you want to "round down", you can use:
Math.floor(2.7);
I am implementing collision detection in my game, and am having a bit of trouble understanding how to calculate the vector to fix my shape overlap upon collision.
Say for example, I have two squares. squareA and squareB. For both of them, I know their xCo, yCo, width and height. squareA is moving however, so he has a velocity magnitude, and a velocity angle. Let's pretend I update the game once a second. I have illustrated the situation below.
Now, I need a formula to get the vector to fix the overlap. If I apply this vector onto the red square (squareA), they should not be overlapping anymore. This is what I am looking to achieve.
Can anyone help me figure out the formula to calculate the vector?
Bonus points if constructed in Java.
Bonus bonus points if you type out the answer instead of linking to a collision detection tutorial.
Thanks guys!
Also, how do I calculate the new velocity magnitude and angle? I would like sqaureA to continue moving along the x axis (sliding along the top of the blue square)
I had an function that looked something like this:
Position calculateValidPosition(Position start, Position end)
Position middlePoint = (start + end) /2
if (middlePoint == start || middlePoint == end)
return start
if( isColliding(middlePont) )
return calculateValidPosition(start, middlePoint)
else
return calculate(middlePoint, end)
I just made this code on the fly, so there would be a lot of room for improvements... starting by not making it recursive.
This function would be called when a collision is detected, passing as a parameter the last valid position of the object, and the current invalid position.
On each iteration, the first parameter is always valid (no collition), and the second one is invalid (there is collition).
But I think this can give you an idea of a possible solution, so you can adapt it to your needs.
Your question as stated requires an answer that is your entire application. But a simple answer is easy enough to provide.
You need to partition your space with quad-trees
You seem to indicate that only one object will be displaced when a collision is detected. (For the case of multiple interpenetrating objects, simply correct each pair of objects in the set until none are overlapping.)
Neither an elastic nor an inelastic collision is the desired behavior. You want a simple projection of the horizontal velocity (of square A), so Vx(t-1) = Vx(t+1), Vy(t-1) is irrelevant and Vy(t+1)=0. Here t is the time of collision.
The repositioning of Square A is simple.
Define Cn as the vector from the centroid of A to the vertex n (where the labeling of the vertices is arbitrary).
Define A(t-1) as the former direction of A.
Define Dn as the dot product of A(t-1) and the vector Cn
Define Rn as the width of A measured along Cn (and extending past the centroid in the opposite direction).
Define Sn as the dilation of B by a radius of Rn.
Let j be the vertex of B with highest y-value.
Let k be the vertex that is most nearly the front corner of A [in the intuitive sense, where the value of Dn indicates that Ck is most nearly parallel to A(t-1)].
Let K be the antipodal edge or vertex of A, relative to k.
Finally, translate A so that k and j are coincident and K is coincident with Sk.
I'm working on a program where I need to traverse a square from greatest distance to shortest distance, when starting from the center and given a maximum range that the object can travel.
The traversal would look something like this, each step is labeled starting from 0 to 35 (sorry for crappy diagram). Max distance would be 3 from center:
I was thinking it could work in two for loops, but I don't think that will work anymore without a ton of if statements. I want it to be semi-efficient if possible.
I don't need any code, just some ideas on how to get it to work (although feel free to post whatever).
Thanks for your help guys.
The simplest approach I can think of would involve 5 loops - one for each direction (all separate loops), and then one for the distance from the outside (which would be the outer-most loop).
The outer-most loop's value would allow us to determine where each loop should start and end.
As a rough draft, I imagine it would look something like this:
for i = 0 to n
for x = i to (n-i-1)
// process matrix[x][i]
for y = i to (n-i-1)
// process matrix[n-i][y]
for x = (n-i) downto i
// process matrix[x][n-i]
for y = (n-i) downto i
// process matrix[i][y]
I'll leave it to you to write the actual code.