I have a bouncing ball application and I have to extend it to prevent overlapping of the balls.
When ball overlaps another, they should move away as in real life.
I have to extend the given MoveBall method:
private void MoveBall()
{
prevX = x;
prevY = y;
x += xVelocity;
y += yVelocity;
// Is there too closed ball?
foreach (Ball ball in parentForm.balls)
{
distance = Math.Sqrt(Math.Pow((double)(ball.prevX - prevX), 2) +
Math.Pow((double)(ball.prevY- prevY), 2));
overlap = ((radius + ball.radius) - distance);// +ball.radius;
if (ball.id != this.id &&
ball.id != lastID &&
overlap > 0)
{
lastID = this.id;
if (xVelocity > 0) // roading right
{
xVelocity = -xVelocity;
x -= xVelocity - ball.xVelocity;
}
else if (xVelocity <= 0) // roading left
{
xVelocity = -xVelocity;
x += xVelocity + ball.xVelocity;
}
if (yVelocity > 0)
{ // going up
yVelocity = -yVelocity;
y -= yVelocity - ball.yVelocity;
}
else if (yVelocity <= 0) // down
{
yVelocity = -yVelocity;
y += yVelocity + ball.yVelocity;
}
}
}
// ***********************************************
// ***************** END MY CODE *****************
if (x > parentForm.Width - 10 - (radius) || x < 0)
{
if (x < 0) x = 0;
if (x > parentForm.Width - 10) x = parentForm.Width - 10 - radius;
xVelocity = -xVelocity;
}
if (y > parentForm.Height - 40 - (radius) || y < 0)
{
if (y < 0) y = 0;
if (y > parentForm.Height - 40) y = parentForm.Height - 40 - (radius);
yVelocity = -yVelocity;
}
}
x,y, xVelocity, yVelocity, radius, prevX, prevY declared as int.
overlap, distance as double.
When 2 overlap, they are getting stuck. Why?
Unfortunately, I can't upload all source code because there are lot of modules.
I'm using Visual C# Express 2010.
As no Question is asked explicitly, I will assume the question "Why are the balls sticking together?"
You have only shown one loop in source code, that's not enough ;-) To check all possible collisions, you need to check n*(n-1)/2 possible collisions. That is normally done with two loops. You have to put in careful measures to avoid handling the same collision twice.
The reason that your balls get stuck is that you handle the same collision multiple times. For example two balls colliding exactly horizontal: The left one has velocity 5 and x-position of 100. The other one shall have a position of 110 and velocity of -6. When the collision happens:
x is set to 105.
Collision detected: x is set to 104 and velocity to -5.
The other Ball handles the same collision:
He moves according to his velocity to position 104.
Collision handling: His velocity becomes 6 and position becomes 105.
The balls were at 100 and 110 resp. and have been moved to 104 and 105. While the velocities are now pointing away from each other, the collision handling in the following step will invert them again. So the positions are close together and the velocities are changing sign every frame. The balls seem "stuck to each other".
I hope the answer helps you to understand your problem. For a better implementation of an elastic collision (that handles each collision exactly once) look here: Ball to Ball Collision - Detection and Handling
Having stumbled upon similar issues when I made my first attempts at collision detection algorithms, I'll try to describe what I think is the problem here.
Maybe the balls move fast enough so that, before collision is even detected by your code, they are already partially "inside" each other. When collision detection comes and notices that, it does what it's supposed to do: change the planned trajectories of the objects according to the details of the collision that just happened. The problem is that, because these objects got sort-of "merged" before collision detection caught them, they can't get unstuck because collision detection is fired again, trapping them with each other.
If this is the source of the problem, then maybe the above code would work with a small enough velocity vector. Of course, that's not a real solution, but if it does work for very small velocities, it probably confirms my hypothesis and you have some idea regarding how to proceed.
Related
Hi im building a chess game in Unity and my issue is that the queen can move above a friendly piece.
When a piece is selected, an array of legal moves on the board is generated
legalMoves = piece.Move();
I chose to work with vectors here so any move within the list is a 2d Vector.
Then in a loop, I check if any of the moves are within the boundaries of the board and if the move would place the piece on another friendly piece. If it does, then discard that move but my problem is that it should discard all the moves within that direction.
For example: if the queen is on (3,3) and there is a pawn on (3,5), then all the moves (3,6), (3,7) ... should be discarded but not (3,4).
Similarly, if the black queen is on (7,7) and a pawn is on (7,5) then all moves (7,4), (7,3), (7,2) .. should be discarded but not (7,6).
My intuition here was that when a vector has a friendly piece on it, check the direction and the length of all my legal moves against it:
if (dir.normalized.Equals(temp[j].normalized) && dir.SqrMagnitude() < temp[j].SqrMagnitude())
The idea was to remove all the vectors from the legalmoves with the same direction but with greater length, however this doesn't really seem to work because the normalized vectors will not be equal.
Here is the relevant code
foreach (var dir in legalMoves)
{
if (0 <= dir.x && dir.x <= 7 && 0 <= dir.y && dir.y <= 7)
{
//TODO shrink this to 1d array search
if (board[(int) dir.x, (int) dir.y].getPiece() == null)
{
Instantiate(trail, dir, Quaternion.identity);
}
else
{
List<Vector2> temp = legalMoves.ToList();
GameObject[] trails = GameObject.FindGameObjectsWithTag("bullet");
for (int j = 0; j< temp.Count; j++)
{
if ( dir.normalized.Equals(temp[j].normalized) && dir.SqrMagnitude() < temp[j].SqrMagnitude())
{
foreach(var t in trails)
{
// remove trail
if (t.transform.position.Equals(temp[j])) Destroy(t);
}
// remove the move with the same direction
temp.Remove(temp[j]);
}
}
temp.Remove(dir);
legalMoves = temp.ToArray();
}
}
}
here is my problem visualized chess collision issue
Ok, maybe there could be a better solution, however, the way I managed to do it is the following:
Our queen = q is at (3,0) , obstructed piece = k is at (3,6),
If we calculate the offset from k to q we always get an axis which is 0 (either x or y ) in this case it is x.
Since we know an axis is 0 we can check which one is 0 using a boolean, in this case it is x, and simply discard all the legal moves which are on x and above 6 or below 6 for black pieces.
I haven't thought about how to do it if a piece obstructs horizontally, however I'm sure its just a matter of adding/ substracting the right coordinates as above.
My game involves ships moving and rotating around a target (i.e., an enemy ship). Rotation depends on whether the user wants to rotate by port/starboard, or just by the closest side to the enemy.
The problem:
The angles are being wrapped with MathHelper.WrapAngle(). Keeping the angles between PI and -PI works great, until the rotating ship gets to the point where -3.141 becomes 3.141 (and vice versa). For example, the ship is rotating to port correctly, then when it hits this line it flips over starboard, then back to port again, then starboard again and so on!
I would be very grateful if the community could point out:
What I can do to make the ship rotate logic work correctly when going over the PI/-PI wrap 'barrier'
Point out any inefficiencies in my code (I'm sure there are many, and I'm sure there are many other ways to do this more efficiently)
Link to any relevant articles or tutorials that can help me overcome this issue (this is my first game)
Additional Information:
Ship.ShipMoveState.NoMoveRotate is essentially a flag that tells the ship to rotate (starting at 100th of max speed up until we hit max speed) either port or starboard, whichever is closest. The ship rotates to these sides as that is where the weapons are located. ShipMoveState.AwaitFurtherOrders is tells the ship to rotate depending on the difference in angle between closest side (port/starboard) and angle to enemy.
ShipCompartment primeCompartment = TargetShip.CenterCompartment;
if (FireState == ShipFireState.FireAtTarget)
primeCompartment = TargetCompartment;
// If ship is to the left of target, below will work
Vector2 distanceToDestination = primeCompartment.Position - CenterCompartment.Position;
float angleToEnemy = (float)Math.Atan2(distanceToDestination.Y, distanceToDestination.X);
angleToEnemy = MathHelper.WrapAngle(angleToEnemy);
CenterCompartment.Rotation = MathHelper.WrapAngle(CenterCompartment.Rotation);
float portBatteryAngle = MathHelper.WrapAngle(CenterCompartment.Rotation - Helpers.RightAngle);
float starboardBatteryAngle = MathHelper.WrapAngle(CenterCompartment.Rotation + Helpers.RightAngle);
float allowance = 0.005f;
bool portIsClosest = false;
switch (primaryFacing)
{
case PreferredFacing.None:
// If port battery not facing enemy
if (angleToEnemy > (MathHelper.WrapAngle(portBatteryAngle + allowance))
|| angleToEnemy < (MathHelper.WrapAngle(portBatteryAngle - allowance)))
{
// And starboard battery not facing either
if (angleToEnemy > (MathHelper.WrapAngle(starboardBatteryAngle + allowance))
|| angleToEnemy < (MathHelper.WrapAngle(starboardBatteryAngle - allowance)))
MoveState = Ship.ShipMoveState.NoMoveRotate;
else
MoveState = ShipMoveState.AwaitFurtherOrders;
}
else
{
portIsClosest = true;
MoveState = ShipMoveState.AwaitFurtherOrders;
}
if (MoveState == ShipMoveState.AwaitFurtherOrders)
{
float diff = 0f;
if (portIsClosest)
diff = angleToEnemy - portBatteryAngle;
else
diff = angleToEnemy - starboardBatteryAngle;
RotateShip(diff);
}
else if (MoveState == Ship.ShipMoveState.NoMoveRotate)
{
// Turn to port (if target is between 6 and 9 o'clock)
if (angleToEnemy < portBatteryAngle)
RotateShip(-MaxRotation / 100);
// Turn to starboard (if target is between 3 and 6 o'clock)
else if (angleToEnemy > starboardBatteryAngle)
RotateShip(MaxRotation / 100);
else
{
if (angleToEnemy > portBatteryAngle && angleToEnemy < starboardBatteryAngle)
{
// Turn to starboard (if target is between 9 and 12 o'clock)
if (angleToEnemy < CenterCompartment.Rotation)
RotateShip(MaxRotation / 100);
// Turn to port (if target is between 12 and 3 o'clock)
else
RotateShip(-MaxRotation / 100);
}
}
}
break;
}
Please let me know if you require any further information. Thank you very much for your assistance.
Instead of, for instance,
a < wrap(b-c)
use
0 < wrap(b-c-a)
or
0 > wrap(a-b+c)
This makes it a little less readable, but is the correct way to compare (supposedly small) angle differences.
I am trying to create a jump method in XNA, but I am facing a lot of problems, it doesn't work for me, I've been trying doing it for like 2 hours long and still no "luck". Can anybody give me a code sample, or at least a direction?
Note: I want the jump to be realistic, using gravity and such.
Thank you!
I erased all my work but here's the latest I tried, I know it shouldn't work for sure, but still..
public void Jump(GameTime gameTime)
{
float currentTime = (float)0.1;
if (position.Y == 200)
{
position.Y += velocity.Y*currentTime -gravity * (float)(Math.Pow(currentTime,2)) / 2;
}
if (position.Y == 200 + jumpHeight)
{
position.Y -= velocity.Y * currentTime - gravity * (float)(Math.Pow(currentTime, 2)) / 2;
}
}
I see that you've learned the equations of motion from your code (which I don't think was in place when I made my comment). However you're a little off, your issue is with your time variable. You're passing in your game time, but then using .1f for your time variable. What you really want for your time variable is the time since you started the jump. Further, position.Y is unlikely to ever be exactly equal to 200 or 200 + jumpHeight. It's a float (I assume), so you can never trust that it'll be a nice round number. If you want to specify an exact maximum jump height, you'll have to perform some equations before and set your velocity.Y accordingly (solve for when velocity equals 0 i.e. the top of your jump).
So, to fix your original code I think something like this will work, albeit totally untested:
public void Jump(GameTime gameTime)
{
if(jumping && position.Y > groundLevelAtPlayer) {
//Get the total time since the jump started
float currentTime = gameTime.totalTime - character.timeofjumpStart;
//gravity should be a negative number, so add it
position.Y += (velocity.Y * currentTime)
+ (gravity * ((float)(Math.Pow(currentTime, 2)) / 2));
}
else
{
jumping = false;
}
}
How exactly do you want to make your player jump? Since you mentioned realism I am assuming that you want your player to jump and move across both the X and Y axis, so that it will move along an arc(parabola if you will).
If that is what you are after, you will need to replicate projectile motion. This previous SO thread contains various methods in which you could implement such motion.
Edit:
You should be able to use the same equations presented. For vertical jumps, the angle will be 90 degrees, or pi/2 if you are working with radians. If you are pressing your direction keys, you will have to use the same equations. The angle at which you want your player to start the jump will have to be selected by yourself. Usually maximum range is obtained at an angle of 45 degrees (pi/4) assuming that the same force is used, so your option is really between 0 and 45 degrees.
I'm working on an assignment for uni where I have to create a Breakout game in Visual Studio 2010 using C# Win Forms. At the moment, I am concentrating on there being only one brick to be destroyed so I have the mechanics down before expanding on it.
To clarify about my current program: I am using a picture box as a Graphics object and a timer to create the animation effect. The ball can skip, at each frame, between 1 and 10 pixels — this is part of creating a random starting vector for the ball.
This works fine until it comes to checking if the ball has 'hit' the brick I have drawn. What I have is an if statement that checks if the ball is at any of the coordinates on the picture box that corresponds to the outline of the brick. I know that the logic is fine because it works some of the time. However, because of the variation in the 'jumping' of the ball's position, I need to add a buffer area of +/- 5 pixels to my if statement.
This is where the problem arises, because my if statement (two, really) is really complicated as it is:
// Checks if ball hits left side or top of brick
if (((x >= brickX) && (x <= (brickX + 50)) && (y == brickY)) ||
((y >= brickY) && (y <= (brickY + 20)) && (x == brickX)))
{
brickHit = true;
}
// Check if ball hits right side or bottom of brick
else if ((((x >= brickX) && (x <= brickX + 50)) && (y == (brickY + 20))) ||
(((y >= brickY) && (y <= brickY + 20)) && (x == brickX + 50)))
{
brickHit = true;
}
For clarification: x and y are the coordinates of the ball and brickX and brickY are the coordinates of the top-left corner of the rectangle brick (which is 50 pixels wide, 10 pixels high).
Is there any way to simplify the above if statements? If I can make them simpler, I know it'll be much easier to add in the 'buffer' (which only needs to be 5 pixels either side of the brick's outline' to allow for the ball's change in position).
If further clarification is needed, please ask — I'm writing this question at 5:12am so I know I might be a little unclear.
One way you could possible simplify this (and I may be misunderstanding your spec), but you can make a Rectangle out of the bounds of the brick and check the Contains for your x,y point.
Rectangle rec = new Rectangle(brickX, brickY, 50, 20);
rec.Offset(-5, -5);
rec.Inflate(10, 10);
if (rec.Contains(new Point(x,y))
{
brickHit = true;
}
brickHit = new Rectangle(brickX,brickY,50,20).Contains(x,y);
Adding a buffer:
int buffer = 5;
brickHit = new Rectangle(brickX,brickY,50,20).Inflate(buffer,buffer).Contains(x,y);
The Rectagle class can come in handy sometimes.
This worked for me:
var rect1 = new System.Drawing.Rectangle(pictureBox1.Location,
pictureBox1.Size);
var rect2 = new System.Drawing.Rectangle(pictureBox2.Location,
pictureBox2.Size);
if (rect1.IntersectsWith(rect2))
{
//code when collided
}
I am stuck trying to figure out how to alter my collision detection to work correctly, i got all my wall objects stacked inside a List and then when the player moves i loop thru each wall object and call the DetectCollision method, this returns true or false depending on if the object is inside the wall or not.
Wall detect collision (X- and Y-coordinate is the position of the wall)
public bool DetectCollision(float x, float y)
{
if ((x >= this.XCoordinate && x <= (this.XCoordinate + this.BlockWidth)) && (y >= this.YCoordinate && y <= (this.YCoordinate + this.BlockHeight)))
return true;
else
return false;
}
So in my player function when the player tries to move, i add the movement to a temporary X,Y coordinate and check if those Collide against the wall, if they do nothing happens, otherwise i move the player.
But i have noticed that it doesn't work as it should be, if i add a piece of wall inside of the gamefield it only checks the bottom right corner for collision detection?
Player movement method:
float x, y;
if (direction == Direction.E)
{
x = LiveObjects.player.XCoordinate - MovementSpeed;
y = LiveObjects.player.YCoordinate;
}
else if (direction == Direction.W)
{
x = LiveObjects.player.XCoordinate + MovementSpeed;
y = LiveObjects.player.YCoordinate;
}
else if (direction == Direction.N)
{
x = LiveObjects.player.XCoordinate;
y = LiveObjects.player.YCoordinate - MovementSpeed;
}
else
{
x = LiveObjects.player.XCoordinate;
y = LiveObjects.player.YCoordinate + MovementSpeed;
}
if (GameMechanics.DetectWallCollision(x, y) || GameMechanics.DetectWallCollision((x + LiveObjects.player.BlockWidth), (y + LiveObjects.player.BlockHeight))
{
OnPlayerInvalidMove(null, new PlayerEventArgs());
return;
}
and the loop for DetectWallCollision is just:
foreach (Wall wall in LiveObjects.walls)
{
if (wall.DetectCollision(x, y))
return true;
}
return false;
Any ideas?
I'm assuming there isn't anything in your world that is infinitely small (i.e. is the size of a pixel). To have true bounding box collision, you've got to consider the size of both objects, not just one.
boolean intersectsEntity(Entity e)
{
return (e.position.x <= position.x + size.x) &&
(e.position.y <= position.y + size.y) &&
(e.position.x + e.size.x >= position.x) &&
(e.position.y + e.size.y >= position.y);
}
That's of course assuming an Entity has a vector for its position and for its size. So size.x == width, and size.y == height.
There is something that disturbs me, you said that the DetectCollision method gets the position of the wall - but if I interpret your code correctly you hand to the DetectWallCollision the x and y parameter which is the position (after movement) of the player and hand that position down to the DetectCollision method...
have you debugged your code to see what coordinates are passed to the collision methods and traced the routes your if-statements are going?
if it is not possible to debug your code for whatever reason - write a trace file - I think the solution will fall into your lap ;)
Your east and west are the wrong way around. With a coordinate system of 0,0 at the top left, increasing positively as you move down or to the right, then a movement West would normally mean a movement left, which means decreasing values of X, and the reverse for East. You are doing the opposite.