[Home]History of Marshmallow/CodeSnippets

Robo Home | Changes | Preferences | AllPages


Revision 5 . . (edit) May 17, 2003 1:28 EST by PEZ
Revision 3 . . March 7, 2003 13:03 EST by Bienator [PEZ and no one else updated this page.]
  

Difference (from prior major revision) (minor diff)

Changed: 1c1,4
How Marshmallow checks if its VirtualBullets has hit
* /RobotUtilsCode?




How Marshmallow checks if its VirtualBullets has hit or missed

Changed: 8,9c11,12
private Ellipse2D vincinity = new Ellipse2D.Double();
private double precision = 20.0;
private Point2D oldRobotLocation?;
private double radius = 20.0;

Changed: 11,17c14,54
boolean hasHit() {
...
vincinity.setFrameFromCenter?(enemyLocation,
new Point2D.Double(enemyLocation.getX() - precision, enemyLocation.getY() - precision));
if (vincinity.contains(bulletLocation)) {
...
return true;
boolean hasHit() {
// calculate bulletLocation here
if (bulletLocation.distance(enemy.location) < radius) {
active = false;
return true;
}
else {
return false;
}
}

boolean hasMissed() {
// calculate bulletLocation here
if (oldRobotLocation?.distance(bulletLocation) > oldRobotLocation?.distance(enemy.location)) {
active = false;
return true;
}
else {
return false;
}
}
</pre>
I used to calculate a circle (Ellipse2D) around the enemyLocation and check to see if the bulletLocation was contained in that circle, but suddenly realised this way is simpler and achives the exact same results. To calculate the bulletLocation we need:
* bulletBearing
* bulletVelocity
* firedTime
* nowTime
* oldRobotLocation
With these we can:

bulletDistance = bulletVelocity * (nowTime - firedTime);
X = oldRobotLocation.getX() + sin(Math.toDegrees(bulletBearing)) * bulletDistance;
Y = oldRobotLocation.getY() + cos(Math.toDegrees(bulletBearing)) * bulletDistance;
bulletLocation.setLocation(X, Y);

Of course some of this should be encapsulated into a generic function. Marshmallow has a robot utilities class with a function like this:
<pre>
static void setLocationFromVector?(double angle, double length, Point2D sourceLocation, Point2D targetLocation) {
double X = sourceLocation.getX() + sin(angle) * length;
double Y = sourceLocation.getY() + cos(angle) * length;
targetLocation.setLocation(X, Y);

Changed: 19,20c56,64
else {
return false;
</pre>
And sin/cos functions like these:
<pre>
static double cos(double a) {
return Math.cos(Math.toRadians(a));
}

static double sin(double a) {
return Math.sin(Math.toRadians(a));

Removed: 22d65
}

Changed: 24c67
The code not shown isn't TopSecret? as such, its just not relevant to the example. Feel free to use this code. Give me credit if you think I deserve it. -- PEZ
Feel free to use this code. Give me credit if you think I deserve it. -- PEZ

Robo Home | Changes | Preferences | AllPages
Search: