patternjavaMinor
Checking a circle against some bounds in a collision detector
Viewed 0 times
circlecollisioncheckingagainstsomedetectorbounds
Problem
First, some code. I have a class, Circle:
The Circle's tick() method is called once per cycle, which updates the position members (xCurrent & yCurrent). I have another class, aptly named CollisionDetector, whose responsibility is to assess Circle positions and make changes under certain criteria. In that particular class, I am currently working on the following method:
```
public void checkBounds(Circle circle, int fieldWidth, int fieldHeight){
int radius = circle.getRadius();
int boundWidth = fieldWidth - radius;
int boundHeight = fieldHeight - radius;
float xPosition = circle.getXPosition();
float yPosition = circle.getYPosition();
if(xPosition >= boundWidth){
if(xPosition != boundWidth)
circle.setXPosition(boundWidth + boundWidth - xPosition);
circle.setXVector(-circle.getXVe
package aag;
public class Circle{
private final int RADIUS;
private float xVector;
private float yVector;
private float xCurrent;
private float yCurrent;
public Circle(int radius, int xCurrent, int yCurrent){
RADIUS = radius;
this.xCurrent = xCurrent;
this.yCurrent = yCurrent;
xVector = 0;
yVector = 0;
}
public float getXVector(){
return xVector;
}
public void setXVector(float xVector){
this.xVector = xVector;
}
public float getYVector(){
return yVector;
}
public void setYVector(float yVector){
this.yVector = yVector;
}
public float getXPosition(){
return xCurrent;
}
public void setXPosition(float xCurrent){
this.xCurrent = xCurrent;
}
public float getYPosition(){
return yCurrent;
}
public void setYPosition(float yCurrent){
this.yCurrent = yCurrent;
}
public int getRadius(){
return RADIUS;
}
public void tick(){
xCurrent += xVector;
yCurrent += yVector;
}
}The Circle's tick() method is called once per cycle, which updates the position members (xCurrent & yCurrent). I have another class, aptly named CollisionDetector, whose responsibility is to assess Circle positions and make changes under certain criteria. In that particular class, I am currently working on the following method:
```
public void checkBounds(Circle circle, int fieldWidth, int fieldHeight){
int radius = circle.getRadius();
int boundWidth = fieldWidth - radius;
int boundHeight = fieldHeight - radius;
float xPosition = circle.getXPosition();
float yPosition = circle.getYPosition();
if(xPosition >= boundWidth){
if(xPosition != boundWidth)
circle.setXPosition(boundWidth + boundWidth - xPosition);
circle.setXVector(-circle.getXVe
Solution
I will just scetch how to approach your problem, but leave the gory implementation details up to you:
-
to avoid duplicate code which differs only in the direction in comparison
-
to avoid duplicate code which differs only in the direction in comparison
ab, you refactor that part of the code out into a function with an additional int parameter sign. sign should be either -1 or +1, and the comparison can now be written as sign*a
-
to avoid the repetition of X an Y, one possible solution is to implement your xy-vectors in terms of two dimensional arrays:
private float[] vector = new float[2];
private float[] current =new float[2]
Again, now it will be possible to loop over the vector components when operating on them, instead of writing almost the same code twice. Of course, that comes for the price of loosing a little bit of readability, and for the price of loosing a little bit performance. In fact, the last two points are the reason why I personally prefer a different solution for this category of problems:
First, create a fairly simple class named Vector2D, with attributes in terms of X and Y. This class should provide all basic 2D vector operations, like adding/substracting 2D vectors, multiplication with a scalar etc. There will be a little bit of similar looking code for all X and Y operations inside the implementation of this class, but not too much. Now, implement your Circle class in terms of Vector2D:
`private Vector2D vector;`
`private Vector2D current;`
Do not provide any methods for your Circle with individual X and Y components.
Of course, the latter approach will not directly decrease the size of your checkBounds` function much, since that is an operation which cannot be implemented easily with classic 2D vector arithmetics, and it actually has to deal with the X and Y components individually. But it will most probably decrease the size of a lot of other methods inside your program.Code Snippets
`private Vector2D vector;`
`private Vector2D current;`Context
StackExchange Code Review Q#110548, answer score: 2
Revisions (0)
No revisions yet.