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

(New section: Off the top of my head.. but could this work) |
|||

Line 25: | Line 25: | ||

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) | 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. | ||

+ | |||

+ | <pre> | ||

+ | 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).mul(a)+b*b))/a; | ||

+ | return tPos.add(tVel.mul(c)); | ||

+ | } | ||

+ | } | ||

+ | </pre> |

## Revision as of 22:53, 13 May 2009

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.

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).mul(a)+b*b))/a; return tPos.add(tVel.mul(c)); } }