How to make my image randomly move?

My image is rotated in a counterclockwise direction. Now, I want it to move when I touch the wall or anytime and anywhere. The problem is that I can’t do it, please help me to solve this problem.

This is my code:

private double x;
private double y;
private double speed;

public void move(long dt)
{
double dt_s = dt / 1e9;
double dx = speed * dt_s;
double dy = speed * dt_s ;

final double right_border = 100;
final double up_border = 100;
final double down_border = 0.0;
final double left_border = 0.0;
< br />
x += dx;

if (x >= right_border)
{
x = right_border;
if (y >= down_border) )
{
y += dy;
}

}
if (y> up_border)
{
y = up_border;
if (x >= left_border)
{
speed *= -1;
}

}
if (x < = left_border)
{
x = left_border;
if (y <= u p_border)
{
y += dy;
}

}
if (y {
y = down_border;
if (x <= right_border)
{
speed *= -1;
}

}

}

First of all, you must solve the problem of the directionlessness of your class – you have speed, but Your direction is fixed at 45 degrees northeast (increments x and y are the same).

Add a direction to your class as follows:

< pre>…
private double speed;
private double angle; // in radians-makes math easier

public void move(long dt) {
. ..
double dx = speed * dt_s * Math.sin(angle);
double dy = speed * dt_s * Math.cos(angle);
… < p>Now moving in a random direction:

myObject.setAngle(Math.PI * 2 * Math.random()); // Math.PI * 2 = 360 degrees< /pre> 

If you hit a wall, you must limit the angle to an angle away from the wall you are hitting. I will leave it to you, but it will take the following form:

myObject.setAngle(minAngle + ((maxAngle-minAngle) * Math.random()));

My image is reversed Clockwise rotation. Now, I want it to move during touching the wall or anytime and anywhere. The problem is that I can’t do it, please help me to solve this problem.

This is my code:

p>

private double x;
private double y;
private double speed;

public void move(long dt)
{
double dt_s = dt / 1e9;
double dx = speed * dt_s;
double dy = speed * dt_s;

final double right_border = 100;< br /> final double up_border = 100;
final double down_border = 0.0;
final double left_border = 0.0;


x += dx;

if (x >= right_border)
{
x = right_border;
if (y >= down_border)
{
y += dy;< br /> }

}
if (y> up_border)
{
y = up_border;
if (x >= left_border)
{
speed *= -1;
}

}
if (x <= left_border)
{
x = left_border;
if (y <= up_border)
{
y += dy;
}

}
if (y {
y = down_border;
if (x < = right_border)
{
speed *= -1;
}

}

}

< p>

First, you must solve the problem of your class's directionlessness-you have speed, but your direction is fixed at 45 degrees northeast (increments x and y are the same).

Add directions to your class as follows:

...
private double speed;
private double angle; // in radians-makes math easier

public void move(long dt) {
...
double dx = speed * dt_s * Math.sin(angle);
double dy = speed * dt_s * Math.cos(angle);
...

Now go in a random direction:

myObject .setAngle(Math.PI * 2 * Math.random()); // Math.PI * 2 = 360 degrees

If you hit a wall, you must limit the angle to the distance away from the wall you are hitting Angle. I will leave it to you, but it will take the following form:

myObject.setAngle(minAngle + ((maxAngle-minAngle) * Math.random()) );

Leave a Comment

Your email address will not be published.