Difference between revisions of "Talk:Linear Targeting/Buggy Implementations"

From Robowiki
Jump to navigation Jump to search
(snippets need some reformatting)
 
(add Buggy Implementation #5 discussion)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
These code snippets should be modified to not have super long lines. Also, there's no reason for the last one to be indented across the whole snippet.  --[[User:Voidious|Voidious]] 19:43, 14 November 2007 (UTC)
 
These code snippets should be modified to not have super long lines. Also, there's no reason for the last one to be indented across the whole snippet.  --[[User:Voidious|Voidious]] 19:43, 14 November 2007 (UTC)
 +
 +
<syntaxhighlight>
 +
double absBearing=e.getBearingRadians()+getHeadingRadians();
 +
 +
double eX=getX() + e.getDistance() * Math.sin(absBearing);
 +
 +
double eY=getY() + e.getDistance() * Math.cos(absBearing);
 +
 +
eXChange=(eX-oldX)*(e.getDistance()/11);
 +
 +
eYChange=(eY-oldY)*(e.getDistance()/11);
 +
 +
oldX=getX() + e.getDistance() * Math.sin(absBearing);
 +
 +
oldY=getY() + e.getDistance() * Math.cos(absBearing);
 +
 +
double enemyLocation=robocode.util.Utils.normalAbsoluteAngle(Math.atan2((eX-getX())+eXChange,(eY-getY())+eYChange));
 +
 +
setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(enemyLocation-getGunHeadingRadians()));
 +
 +
setFire(3);
 +
</syntaxhighlight>
 +
 +
 +
I'm trying to make a gun that makes use of code similar to this, but for some reason this doesn't work exactly right... Does anyone know if this is a math problem or a programming problem?--[[User:CrazyBassoonist|CrazyBassoonist]] 22:43, 9 April 2009 (UTC)
 +
 +
== Off the top of my head.. but could this work ==
 +
 +
Not sure if this could actually work, untested, and here it is in uncompiled code. I got this from my 3D version of my linear targeting, translated into java (I am sure I screwed up somewhere :P). Given locations of the two robots x and y, and your bullet and enemies velocities along the two axis, should get the final position of intersection in x and y coordinates. --[[User:Chase-san|Chase]] 20:55, 13 May 2009 (UTC)
 +
 +
<syntaxhighlight>
 +
class vect {
 +
double x;
 +
double y;
 +
public vect(double nx, double ny) {
 +
x = nx;
 +
y = ny;
 +
}
 +
public double dot(vect i) {
 +
return i.x*x+i.y*y;
 +
}
 +
public vect sub(vect i) {
 +
return new vect(x-i.x,y-i.y);
 +
}
 +
public vect add(vect i) {
 +
return new vect(x+i.x,y+i.y);
 +
}
 +
public vect mul(double i) {
 +
return new vect(x*i,y*i);
 +
}
 +
public vect intercept(vect cPos, vect ePos, vector eVel, double bVel) {
 +
vect rPos = ePos.sub(cPos);
 +
double a = bVel*bVel - tVel.dot(tVel);
 +
double b = rPos.dot(eVel);
 +
double c = (b + Math.sqrt(rPos.dot(rPos)*a+b*b))/a;
 +
return tPos.add(tVel.mul(c));
 +
}
 +
}
 +
</syntaxhighlight>
 +
 +
: Actually now that I actually look at the other code this is like buggy implimentation #3 and the nano targeting (simple trig). --[[User:Chase-san|Chase]] 21:07, 13 May 2009 (UTC)
 +
 +
== Buggy implementation #5 ? ==
 +
 +
Hey, I've been suffering with trig, and I need help.  I've been trying to create a linear targeting [[Virtual Gun]] for [[BulletSimBot]], because the one it has at the moment is slightly inaccurate, and now it's many, many times more inaccurate...  I need help!  What's wrong with this?  It ends up shooting opposite of where I want it to!
 +
<syntaxhighlight>
 +
//This calculates the angle the scanned robot would shoot at if he were using linear targeting, but fails miserably.
 +
public double linear(ScannedRobotEvent e, double power) {
 +
double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
 +
double angleToFire = Utils.normalRelativeAngle(absoluteBearing + (e.getVelocity() * Math.asin(Math.sin(e.getHeadingRadians() -
 +
absoluteBearing) / (20 - power * 3))));
 +
return angleToFire;
 +
}
 +
</syntaxhighlight>
 +
 +
Any help?  PS:  Am I supposed to move all of the numbers or something so that I'm number one and so on?  --[[User:Awesomeness|Awesomeness]] 03:11, 23 January 2010 (UTC)
 +
 +
: No, you should actually move this to the bottom and label it number five. But I think think this belong to this page at all. --[[User:Nat|<span style="color:#099;">Nat</span>]] [[User talk:Nat|<span style="color:#0a5;">Pavasant</span>]] 12:02, 23 January 2010 (UTC)
 +
 +
Well, that "Utils.normalRelativeAngle" should be "Utils.normalAbsoluteAngle" since angleToFire is an absolute angle, unlike gunturn. Other than that it looks right to me. If that doesn't fix it, I'd suspect whatever code is making use of the 'angleToFire'. --[[User:Rednaxela|Rednaxela]] 03:29, 23 January 2010 (UTC)
 +
 +
::Changing it didn't seem to change the result at all.  Can you test it?  It's not working...  And I tested the bullet simulator, it works fine.  --[[User:Awesomeness|Awesomeness]] 04:03, 23 January 2010 (UTC)
 +
 +
I think you want to calculate the angle **enemy** will shoot at you, not the angle you are shooting at enemy. So...
 +
<syntaxhighlight>
 +
//This calculates the angle the scanned robot would shoot at if he were using linear targeting, but fails miserably.
 +
public double linear(ScannedRobotEvent e, double power) {
 +
double absoluteBearing = Utils.normalAbsoluteAngle(getHeadingRadians() + e.getBearingRadians() + Math.PI);
 +
double angleToFire = Utils.normalAbsoluteAngle(absoluteBearing + (getVelocity() * Math.asin(Math.sin(e.getBearingRadians()) / (20 - power * 3))));
 +
return angleToFire;
 +
}
 +
</syntaxhighlight>
 +
 +
Well, in case you confused; since the targeting formula:
 +
 +
:<math>
 +
velocity \times \sin (targetHeading - targetAngle) \over {bulletVelocity}
 +
</math>
 +
 +
:(TeX seems to be error, here is plain text version: ''velocity &times; ''sin''(targetHeading - targetAngle) / bulletVelocity'')
 +
 +
And thus the angle from enemy to you is your angle to enemy plus PI rad (= 180 degrees), yield <code>targetAngle = getHeadingRadians() + e.getBearingRadians() + Math.PI</code>
 +
 +
<code>targetHeading</code> is your own heading, since you are targeting your own robot. Put those things into the formula above will result in this ''sin'' expressions:
 +
 +
<code>sin(getHeadingRadians() - getHeadingRadians() - e.getBearingRadians() - Math.PI) == sin(-e.getBearingRadians() - Math.PI) == Math.sin(e.getBearingRadians())</code>.
 +
 +
Thus the final code being as my code above. --[[User:Nat|<span style="color:#099;">Nat</span>]] [[User talk:Nat|<span style="color:#0a5;">Pavasant</span>]] 12:00, 23 January 2010 (UTC)
 +
 +
:Oh, lol.. I feel so stupid XD    --[[User:Awesomeness|Awesomeness]] 13:10, 23 January 2010 (UTC)

Latest revision as of 05:48, 17 April 2011

These code snippets should be modified to not have super long lines. Also, there's no reason for the last one to be indented across the whole snippet. --Voidious 19:43, 14 November 2007 (UTC)

		double absBearing=e.getBearingRadians()+getHeadingRadians();

		double eX=getX() + e.getDistance() * Math.sin(absBearing);

		double eY=getY() + e.getDistance() * Math.cos(absBearing);

		eXChange=(eX-oldX)*(e.getDistance()/11);

		eYChange=(eY-oldY)*(e.getDistance()/11);

		oldX=getX() + e.getDistance() * Math.sin(absBearing);

		oldY=getY() + e.getDistance() * Math.cos(absBearing);

		double enemyLocation=robocode.util.Utils.normalAbsoluteAngle(Math.atan2((eX-getX())+eXChange,(eY-getY())+eYChange));

		setTurnGunRightRadians(robocode.util.Utils.normalRelativeAngle(enemyLocation-getGunHeadingRadians()));

		setFire(3);


I'm trying to make a gun that makes use of code similar to this, but for some reason this doesn't work exactly right... Does anyone know if this is a math problem or a programming problem?--CrazyBassoonist 22:43, 9 April 2009 (UTC)

Off the top of my head.. but could this work

Not sure if this could actually work, untested, and here it is in uncompiled code. I got this from my 3D version of my linear targeting, translated into java (I am sure I screwed up somewhere :P). Given locations of the two robots x and y, and your bullet and enemies velocities along the two axis, should get the final position of intersection in x and y coordinates. --Chase 20:55, 13 May 2009 (UTC)

class vect {
	double x;
	double y;
	public vect(double nx, double ny) {
		x = nx;
		y = ny;
	}
	public double dot(vect i) {
		return i.x*x+i.y*y;
	}
	public vect sub(vect i) {
		return new vect(x-i.x,y-i.y);
	}
	public vect add(vect i) {
		return new vect(x+i.x,y+i.y);
	}
	public vect mul(double i) {
		return new vect(x*i,y*i);
	}
	public vect intercept(vect cPos, vect ePos, vector eVel, double bVel) {
		vect rPos = ePos.sub(cPos);
		double a = bVel*bVel - tVel.dot(tVel);
		double b = rPos.dot(eVel);
		double c = (b + Math.sqrt(rPos.dot(rPos)*a+b*b))/a;
		return tPos.add(tVel.mul(c));
	}
}
Actually now that I actually look at the other code this is like buggy implimentation #3 and the nano targeting (simple trig). --Chase 21:07, 13 May 2009 (UTC)

Buggy implementation #5 ?

Hey, I've been suffering with trig, and I need help. I've been trying to create a linear targeting Virtual Gun for BulletSimBot, because the one it has at the moment is slightly inaccurate, and now it's many, many times more inaccurate... I need help! What's wrong with this? It ends up shooting opposite of where I want it to!

	//This calculates the angle the scanned robot would shoot at if he were using linear targeting, but fails miserably.
	public double linear(ScannedRobotEvent e, double power) {
		double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
		double angleToFire = Utils.normalRelativeAngle(absoluteBearing + (e.getVelocity() * Math.asin(Math.sin(e.getHeadingRadians() - 
			absoluteBearing) / (20 - power * 3))));
		return angleToFire;
	}

Any help? PS: Am I supposed to move all of the numbers or something so that I'm number one and so on? --Awesomeness 03:11, 23 January 2010 (UTC)

No, you should actually move this to the bottom and label it number five. But I think think this belong to this page at all. --Nat Pavasant 12:02, 23 January 2010 (UTC)

Well, that "Utils.normalRelativeAngle" should be "Utils.normalAbsoluteAngle" since angleToFire is an absolute angle, unlike gunturn. Other than that it looks right to me. If that doesn't fix it, I'd suspect whatever code is making use of the 'angleToFire'. --Rednaxela 03:29, 23 January 2010 (UTC)

Changing it didn't seem to change the result at all. Can you test it? It's not working... And I tested the bullet simulator, it works fine. --Awesomeness 04:03, 23 January 2010 (UTC)

I think you want to calculate the angle **enemy** will shoot at you, not the angle you are shooting at enemy. So...

	//This calculates the angle the scanned robot would shoot at if he were using linear targeting, but fails miserably.
	public double linear(ScannedRobotEvent e, double power) {
		double absoluteBearing = Utils.normalAbsoluteAngle(getHeadingRadians() + e.getBearingRadians() + Math.PI);
		double angleToFire = Utils.normalAbsoluteAngle(absoluteBearing + (getVelocity() * Math.asin(Math.sin(e.getBearingRadians()) / (20 - power * 3))));
		return angleToFire;
	}

Well, in case you confused; since the targeting formula:

<math>

velocity \times \sin (targetHeading - targetAngle) \over {bulletVelocity} </math>

(TeX seems to be error, here is plain text version: velocity × sin(targetHeading - targetAngle) / bulletVelocity)

And thus the angle from enemy to you is your angle to enemy plus PI rad (= 180 degrees), yield targetAngle = getHeadingRadians() + e.getBearingRadians() + Math.PI

targetHeading is your own heading, since you are targeting your own robot. Put those things into the formula above will result in this sin expressions:

sin(getHeadingRadians() - getHeadingRadians() - e.getBearingRadians() - Math.PI) == sin(-e.getBearingRadians() - Math.PI) == Math.sin(e.getBearingRadians()).

Thus the final code being as my code above. --Nat Pavasant 12:00, 23 January 2010 (UTC)

Oh, lol.. I feel so stupid XD --Awesomeness 13:10, 23 January 2010 (UTC)