This is one of the classic Robocode practice questions: Going to the middle of the map. I didn't want to move twice (once for x-axis and once for y-axis), but instead get the bearing towards the middle and move in a single motion after setting my robot's bearing.
To my dismay, it seems to be not working. Instead, the behavior I observed from the robots is that they turn 180 degrees and move ahead for the value of 'r', which is the hypotenuse of the "triangle" towards the middle. The distance seems to be getting smaller each loop, so I think that the formula for 'r' is right. My suspicion is towards the methods setWest() and setEast() I wrote. Their purpose is to set the robot parallel to the horizontal facing either East or West. Then I once again rotate the robot towards the middle by using theta (which is the second iffy part) and move it.
The methods implemented in Robocode are pretty self-explanatory, not mine though. Here is the code I've written:
public class MyFirstRobot extends Robot {
public void setWest(){
// If it's looking NW
if(getHeading() > 270 && getHeading() < 0)
turnLeft(getHeading() - 270);
// If it's looking SE or SW
else if(getHeading() >= 90 && getHeading() <= 270)
turnRight(270 - getHeading());
// If it's looking NE
else if(getHeading() >= 0 && getHeading() < 90)
turnLeft(90 + getHeading());
// If my coding is undercooked spaghetti
else {
System.out.println("Error");
}
}
public void setEast(){
// If it's looking NW
if(getHeading() > 270 && getHeading() < 0)
turnRight(450 - getHeading());
// If it's looking SE or SW
else if(getHeading() >= 90 && getHeading() <= 270)
turnLeft(getHeading() - 90);
// If it's looking NE
else if(getHeading() >= 0 && getHeading() < 90)
turnRight(90 - getHeading());
// If my coding is undercooked spaghetti
else {
System.out.println("Error");
}
}
public void run() {
double x = 0.0;
double y = 0.0;
double r = 0.0;
double theta = 0.0;
while (true) {
x = getBattleFieldWidth() / 2.0 - getX();
y = getBattleFieldHeight() / 2.0 - getY();
r = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
// Find the angle with respect to the horizontal.
theta = Math.atan((Math.toRadians(y) / Math.toRadians(x)));
/*
* Align tank towards the middle
* depending on the "quadrant" it's in.
*/
// 1st Quadrant
if(x < 0 && y < 0){
setWest();
turnLeft(theta);
}
// 2nd Quadrant
else if(x >= 0 && y < 0){
setEast();
turnRight(theta);
}
// 3rd Quadrant
else if(x >= 0 && y >= 0) {
setEast();
turnLeft(theta);
}
// 4th Quadrant
else if(x < 0 && y >= 0) {
setWest();
turnRight(theta);
}
// Move to the middle after the final rotation.
ahead(r);
}
}
Please don't mind the infinite loop, it's just to observe the behavior. run() is the main method of the robot; it is called by default.
I will answer my own question in case someone wonders what the answer is. There were a few fixes:
getHeading() returns a value between 0 <= getHeading() < 360 in degrees. I mistakenly set one of the if conditions to getHeading() < 0, which isn't possible. I also decided to store the heading in a variable to avoid calling getHeading() multiple times. Here is the fix for that part:
public void setWest(){
double heading = getHeading();
// If it's looking NW
if(heading > 270.0 && heading < 360.0)
turnLeft(getHeading() - 270);
// If it's looking SE or SW
else if(heading >= 90.0 && heading <= 270.0)
turnRight(270 - getHeading());
// If it's looking NE
else if(heading >= 0.0 && heading < 90.0)
turnLeft(90 + getHeading());
// If my coding is god awful
else {
System.out.println("West | Error");
}
The difference here being 0.0 for the first check being changed to 360.0 0.
Theta was stored in radians instead of degrees. turnLeft() or turnRight() needs a parameter in degrees for it to work properly. Putting in radians won't give you an error. It also won't work as intended. Theta also had to be stored as a positive number, since setWest() and setEast() already accounts for the direction. It was simply fixed by using Math.abs() and Math.toDegrees(). As shown below:
// Find the abs value of the angle with respect to the horizontal.
theta = Math.abs(Math.toDegrees(Math.atan((Math.toRadians(y) / Math.toRadians(x)))));
If you had some lingering questions about it, I hope this helped.
Related
I have a "physics ball" that can bounce around the screen off the edges which works fine. But I wanted to be able to add boxes and have my ball be able to bounce of those, too. I have tried to create something and it feels like it is quite close, but there is flaw that I understand why it exists, but am unsure on how I can get around it.
if (colliding(ball, block))
{
if (ball.velocity.x > 0)
{
ball.velocity.x *= -ball.restitution;
ball.vector.x = block.vector.x - ball.radius;
}
else
{
ball.velocity.x *= -ball.restitution;
ball.vector.x = block.vector.x + block.width + ball.radius;
}
if (ball.velocity.y > 0)
{
ball.velocity.y *= -ball.restitution;
ball.vector.y = block.vector.y - ball.radius;
}
else
{
ball.velocity.y *= -ball.restitution;
ball.vector.y = block.vector.y + block.height + ball.radius;
}
}
colliding():
boolean colliding(MassEntity ball, Block block)
{
return PVector.dist(ball.vector, block.vector) < ball.radius
|| PVector.dist(ball.vector, new PVector(block.vector.x + block.width, block.vector.y)) < ball.radius
|| PVector.dist(ball.vector, new PVector(block.vector.x + block.width, block.vector.y + block.height)) < ball.radius
|| PVector.dist(ball.vector, new PVector(block.vector.x, block.vector.y + block.height)) < ball.radius
|| (ball.vector.x - ball.radius < block.vector.x + block.width && ball.vector.x + ball.radius > block.vector.x
&& ball.vector.y - ball.radius < block.vector.y + block.height && ball.vector.y + ball.radius > block.vector.y);
}
(I know the if statement is a little monstrous, but I don't know what happened to the formatting honestly)
The issue is that when the ball collides with the rectangle, since the ball is "teleported" to outside of the rectangle (so it doesn't stay inside the rectangle due to the velocity being flipped), it teleports on both axes so pretty much the ball will weirdly teleport to the end of one of the edges.
I just need to somehow make if statements for the respective axes to only be considered in the appropriate situation.
First, compute P, the nearest point to the ball that is in the box :
PVector P = new PVector(
max(min(ball.vector.x, box.vector.x + box.width / 2), box.vector.x - box.width / 2),
max(min(ball.vector.y, box.vector.y + box.height / 2), box.vector.y - box.height / 2)
);
To check if there is a collision, you can check if the distance between P and the center of the ball is smaller than the radius of the ball.
To update the speed of the ball, you can do this :
PVector n = normalize(ball.vector.copy().sub(P));
ball.velocity.sub(n.mult(2 * n.dot(ball.velocity)));
n is the normal vector a the collision's position and to reflect the speed on the surface you have to delete the component of the velocity that is parallel to it. Then you have to add this same component multiplied by -1. As those two operations are the same, you can just do it one time with a factor 2.
A last precision I have to make is that you may need to check if the ball is going away from the box to avoid reversing the speed in that case :
PVector n = normalize(ball.vector.copy().sub(P));
float f = n.dot(ball.velocity);
if (f < 0)
ball.velocity.sub(n.mult(2 * f));
I haven't tested my code so tell me if there is a problem.
So I made a game and I want the enemies to bounce off the wall when they hit in a random x and y speed. However, somehow these little buggers are still getting out of the window. Most the time it works but every like 10ish times it will sneak out the border and I can't figure out how.
#Override
public void tick()
{
x += speedX;
y += speedY;
Random r = new Random();
//BUGS
if(y <= 0 || y >= Game.HEIGHT - 48) //This is correct size of the window edges
{
if(speedY <= 0)
speedY = (r.nextInt(8) + 1);
else
speedY = -(r.nextInt(8) + 1);
}
if(x <= 0 || x >= Game.WIDTH - 32) //This is correct size of the window edges
{
if(speedX <= 0)
speedX = (r.nextInt(8) + 1);
else
speedX = -(r.nextInt(8) + 1);
}
Issues:
Don't re-create Random as it's wasteful and sometimes dangerous. Better to create one Random object and assign it to an instance field of the class, and use it throughout.
Avoid "magic numbers". So instead of y >= Game.HEIGHT - 48, do y >= Game.HEIGHT - WINDOW_EDGES (or something similar)
Don't swap speed as you're doing but instead check for y <= 0 or y >= Game.HEIGHT -SOME_CONSTANT separately, and gear the results based on this finding to avoid getting caught in a speed "trap". This is your main problem in fact.
e.g.,
if (y <= 0) {
speedY = r.nextInt(8) + 1; // change 8 to a constant
} else if (y >= Game.HEIGHT - SOME_CONSTANT) {
speedY = -(r.nextInt(8) + 1);
}
Same for x and speedX
Regarding:
2) I would like to do that but since I have multiple object sizes, I have to change the edges.
Then each object should have a method that returns its edge size (or whatever property is needed), and you should use this, again, not magic numbers
3) I tried swapping and they just shot off the screen.
I don't know what you mean by this or what specific code changes you may have made.
If still stuck, consider creating and posting a valid Minimal Reproducible Example
so recently, we've been assigned to code multiple circles that act like robots in a GUI interface. Basically, a robot simulator.
I've got the code to spawn in multiple circles that can act like robots.
This is my current code for detecting wall collision between the robot and the end of the square:
private void checkCollisions(double maxX, double maxY) {
for (ListIterator<Ball> slowIt = balls.listIterator(); slowIt.hasNext();) {
Ball b1 = slowIt.next();
// check wall collisions:
double xVel = b1.getXVelocity();
double yVel = b1.getYVelocity();
if ((b1.getCenterX() - b1.getRadius() <= 0 && xVel < 0)
|| (b1.getCenterX() + b1.getRadius() >= maxX && xVel > 0)) {
b1.setXVelocity(-xVel);
}
if ((b1.getCenterY() - b1.getRadius() <= 0 && yVel < 0)
|| (b1.getCenterY() + b1.getRadius() >= maxY && yVel > 0)) {
b1.setYVelocity(-yVel);
}
for (ListIterator<Ball> fastIt = balls.listIterator(slowIt.nextIndex()); fastIt.hasNext();) {
Ball b2 = fastIt.next();
final double deltaX = b2.getCenterX() - b1.getCenterX() ;
final double deltaY = b2.getCenterY() - b1.getCenterY() ;
if (colliding(b1, b2, deltaX, deltaY)) {
bounce(b1, b2, deltaX, deltaY);
}
}
}
}
The
b1.setXVelocity(-xVel);
b1.setYVelocity(-yVel);
are the main bits that make the circle bounce back from the wall. However, instead of this, I want the ball to detect the wall and rotate 90 degrees rather than bounce back form the wall like a bouncing ball.
Any help will be fully appreciated or a working piece of code that ca do this for me. I have an AraryList of all the balls called 'balls'.
If needed, I can give source code.
This is what I have so far. But I need each ball to have a sensor attached to them detecting if there a wall ahead.
https://i.stack.imgur.com/XsQvX.png
Assuming you have just square walls:
If the ball hits the right wall for example, you want to remove all x velocity, and then add either positive or negative velocity.
The issue with this is that the robot will end up just going around the outside edges of the map.
I wrote a program to solve the following:
Implement a diffusion limited aggregation simulation on a toroid plane where seeds are randomly created, and particles move randomly. they move if they particles do not land near a seed or a particle. where user inputs seeds (red pixels), particles (black pixels), steps (no or iterations), plane size.
My code is very slow. How can I make it faster?
I randomly created x and y coordinates and drew red pixels (seeds), then randomly created x and y for black pixels (particles), if a black pixel lands where there is a red or black pixel it can stay, otherwise it moves randomly again until there are no more particles . If the pixel lands out of borders like x > border then x=0; if x <1 then x= border. The same for y.
This just means that if it lands on the border I move it to the opposite border. Then checks for the neighboring pixels again. I have an outer loop to create the seeds, and inner loop for the particles. In the inner loop I check for the x,y positions:
//Place, move, and "stick" the particles; stop if either steps or particles = 0
for (int p = 0; p < particles; p++) {
for (int s = 0; s < steps; s++) {
if (xPos > image.getWidth() ) {
do something
}
else if (xPos < 1) {
do something
}
if (yPos > image.getHeight() - 2) {
do something
}
else if (yPos < 1) {
do something
}
else if (xPos > image.getWidth() && yPos > image.getHeight()) {
do something
}
else if (xPos < 1 && yPos < 1) {
do something
}
//If the surrounding pixels' color is not white, make that particle stick.
if (moveValid()) {
image.setRGB(xPos, yPos, 0xFF000000);
}
//Otherwise, move in a random direction
else {
if(xPos == 1 && image.getRGB(size - 2, yPos) != 0xFFFFFFFF){
draw(xPos,yPos);
}
else if(xPos == size - 2 && image.getRGB(1,yPos) != 0xFFFFFFFF){
draw(xPos,yPos);
}
if(yPos == 1 && image.getRGB(xPos, size - 2) != 0xFFFFFFFF){
draw(xPos,yPos);
}
else if(yPos == size - 2 && image.getRGB(xPos,1) != 0xFFFFFFFF){
draw(xPos,yPos);
}
else {
move();
}
}
}
//Regenerate random x and y positions for the next particle
xPos = random.nextInt(size);
yPos = random.nextInt(size);
}
Although the implementation of draw() is not shown, it looks like you're updating a BufferedImage and then rendering it.
The first step is always to profile your existing code, looking for easily implemented optimizations.
The second step is sometimes to set aside the existing code and try a different approach.
You may be able to leverage the Mode-View-Controller pattern, outlined here and discussed here. In particular, let your DLA model evolve on a background thread at full speed, while updating your view at a more sustainable rate. This article suggests several approaches to synchronization and includes a related example that uses javax.swing.Timer to pace the updates.
I've written a program to bounce a ball around a screen. The program as written below does not work (the ball just moves off screen).
However if I declare the boolean variables atHorizontalEdge and atVerticalEdge inside the while loop, it seems to work. Why is that the case? As the booleans are defined for the entire run() method, shouldn't it be callable by the while loop even though its outside the while loop?
import acm.program.*;
import acm.graphics.*;
import java.awt.*;
public class BouncingBallv3 extends GraphicsProgram {
public void run() {
double x = (getWidth() - BALL_SIZE)/2 ; //sets the starting position of ball at center
double y = (getHeight() - BALL_SIZE)/2 ;
GOval ball = new GOval (x, y, BALL_SIZE, BALL_SIZE ); // creates a red ball at center of screen
ball.setFilled(true);
ball.setColor(Color.red);
add (ball);
double dx = 1; //increments by which the ball moves
double dy = 1;
//declares boolean variables to test if ball position is at an edge
boolean atHorizontalEdge = (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ;
boolean atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;
/* while loop keeps the ball moving in direction dx,dy
* if ball reaches a position at any edge, the direction dx or dy changes
*/
while (true) {
if (atHorizontalEdge) { //changes direction of ball if it hits a left/right wall
dx = -dx;
} else if (atVerticalEdge) { //changes direction of ball if it hits a top/bottom wall
dy = -dy;
}
ball.move(dx,dy);
pause (PAUSE_TIME);
}
}
private static final double BALL_SIZE = 50;
private static final int PAUSE_TIME = 5;
}
The issue is not that the declaration of the booleans is outside the while loop. It is that you are checking for your boundraies outside the while loop. Because of this, your condition is never updated and it only checks for the original state of the ball.
You should update the atHorizontalEdge and atVerticalEdge in the loop body after each iteration, I think.
UPDATE:
The while-loop body should be like this,
`
//declares boolean variables to test if ball position is at an edge
boolean atHorizontalEdge = false;
boolean atVerticalEdge = false;
/* while loop keeps the ball moving in direction dx,dy
* if ball reaches a position at any edge, the direction dx or dy changes
*/
while (true) {
atHorizontalEdge = (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0;
atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0;
if (atHorizontalEdge) { //changes direction of ball if it hits a left/right wall
dx = -dx;
} else if (atVerticalEdge) { //changes direction of ball if it hits a top/bottom wall
dy = -dy;
}
ball.move(dx,dy);
pause (PAUSE_TIME);
}`
The reason why it works if you define atHorizontalEdge and atVerticalEdge inside loop is because each iteration these two variables are re-computed (i.e. updated).
atHorizontalEdge and atVerticalEdge can be declared inside or outside the while loop, that's not important.
The important thing is, the following is calculated only once, before the loop starts:
atHorizontalEdge = (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ;
atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;
Therefore atHorizontalEdge and atVerticalEdge will each have the same value from the start to the end of your run method (which is forever).
You obviously want the above two lines to be executed at every iteration in your loop, since they're not going to update by themselves...
while (true) {
atHorizontalEdge = (ball.getX() == getWidth() - BALL_SIZE) || ball.getX() == 0 ;
atVerticalEdge = (ball.getY() == getHeight() - BALL_SIZE) || ball.getY() == 0 ;
...
}
EDIT: Also, it'd be a better idea to check if the x and y were greater than or equal to the width/height, and less than or equal to 0 for 2 reasons:
If you decide to change the increment from 1 you could skip that exact value and cause a bug, but more importantly:
You're using a double and the floating-point representation of the number might not be exactly what you're comparing it to, so == can cause bugs, and the ball might go past the edge and keep going.
ie. ball.getX() >= getWidth() ... ball.getX() <= 0
What Every Computer Scientist Should Know About Floating-Point Arithmetic