NeoGriever: Aus Winkel und Geschwindigkeit, zielkoordinate erreichnen

Beitrag lesen

Okay. Ich habe einen Berechnungsfehler scheinbar drin :/

function coords(x,y) {  
	var self = this;  
	this.x = x;  
	this.y = y;  
	this.toFormatedString = function() {  
		return(self.x + " / " + self.y);  
	}  
}  
function sphere(pos,spd) {  
	var self = this;  
	this.position = new coords(0,0);  
	this.speed = new coords(100,100);  
	if(typeof(pos) !== "undefined") {  
		this.position = pos;  
	}  
	if(typeof(spd) !== "undefined") {  
		this.speed = spd;  
	}  
	this.getWinkel = function() {  
		var distancey = (self.position.y + self.speed.y) - self.position.y;  
		var distancex = (self.position.x + self.speed.x) - self.position.x;  
		return(Math.atan2(distancey,distancex) * (180 / Math.PI));  
	}  
	this.setWinkel = function(newrad) {  
		self.speed = self.getCoordSpeed(newrad,self.getSpeed());  
	}  
	this.getSpeed = function() {  
		var start = new coords(0,0);  
		var ziel = new coords(self.speed.x - self.position.x,self.speed.y - self.position.y);  
		var calced = new coords(Math.abs(start.x - ziel.x),Math.abs(start.y - ziel.y));  
		var distance = Math.sqrt(Math.pow(calced.x,2) + Math.pow(calced.y,2));  
		return(distance);  
	}  
	this.setSpeed = function(newspeed) {  
		self.speed = self.getCoordSpeed(self.getWinkel(),newspeed);  
	}  
	this.getCoordSpeed = function(deg,spd) {  
		var winkel = self.getWinkel();  
		if(typeof(deg) !== "undefined") {  
			winkel = deg;  
		}  
		var geschw = self.getSpeed();  
		if(typeof(spd) !== "undefined") {  
			geschw = spd;  
		}  
		var radiants = winkel * (Math.PI / 180);  
		var position = new coords((Math.cos(radiants) * geschw),(Math.sin(radiants) * geschw));  
		return(position);  
	}  
	this.getTargetCoords = function(deg,spd) {  
		var winkel = self.getWinkel();  
		if(typeof(deg) !== "undefined") {  
			winkel = deg;  
		}  
		var geschw = self.getSpeed();  
		if(typeof(spd) !== "undefined") {  
			geschw = spd;  
		}  
		var position = self.getCoordSpeed(winkel,geschw);  
		return(new coords(position.x + self.position.x,position.y + self.position.y));  
	}  
}  
var sph = new sphere();  
sph.setSpeed(10);  
sph.setWinkel(180);  
alert(sph.speed.toFormatedString()); // Ausgabe: -10 / 1.2246467991473533e-15