|
-
[F9] Vector class help
Hi,
Never been any good with vectors, but I decided to give it a shot and write a vector class, as part of a simple physics engine. But since I'm no good at it, I'm not sure if it's, well, any good. Could someone please have a look and see if the calculations look right, and if there's anything I could/should have done differently? Thanks.
PHP Code:
package com.gamesquid.physicsquid {
// Written by Sven Magnus
public class Vector {
// public variables
// use these in favour of the getters for slightly better performance
// however, don't set them directly unless you know what you're doing
public var _x1:Number=0;
public var _y1:Number=0;
public var _x2:Number=0;
public var _y2:Number=0;
public var _vx:Number=0;
public var _vy:Number=0;
public var _length:Number=0;
public var _angle:Number=0;
// getter functions
public function get x1():Number {
return this._x1;
}
public function get y1():Number {
return this._y1;
}
public function get x2():Number {
return this._x2;
}
public function get y2():Number {
return this._y2;
}
public function get vx():Number {
return this._vx;
}
public function get vy():Number {
return this._vy;
}
public function get length():Number {
return this._length;
}
public function get angle():Number {
return this._angle;
}
public function get degrees():Number {
return this._angle*180/Math.PI;
}
public function get dx():Number {
return this._vx/this._length;
}
public function get dy():Number {
return this._vy/this._length;
}
public function get rx():Number {
return -this._vy;
}
public function get ry():Number {
return this._vx;
}
public function get lx():Number {
return this._vy;
}
public function get ly():Number {
return -this._vx;
}
public function get normal():Vector {
return new Vector(this._x1,this._y1,this.dx,this.dy);
}
public function get right():Vector {
return new Vector(this._x1,this._y1,this.rx,this.ry);
}
public function get left():Vector {
return new Vector(this._x1,this._y1,this.lx,this.ly);
}
// setter functions
public function set x1(value:Number):void {
this._x1=value;
this._vx=this._x2-value;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set y1(value:Number):void {
this._y1=value;
this._vy=this._y2-value;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set x2(value:Number):void {
this._x2=value;
this._vx=value-this._x1;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set y2(value:Number):void {
this._y2=value;
this._vy=value-this._y1;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set vx(value:Number):void {
this._vx=value;
this._x2=this._x1+value;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set vy(value:Number):void {
this._vy=value;
this._y2=this._y1+value;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
public function set angle(value:Number):void {
this._angle=value;
this._vx=this._length*Math.cos(value);
this._vy=this._length*Math.sin(value);
this._x2=this.x1+this._vx;
this._y2=this.y1+this._vy;
}
public function set length(value:Number):void {
this._length=value;
this._vx=value*Math.cos(this._angle);
this._vy=value*Math.sin(this._angle);
this._x2=this.x1+this._vx;
this._y2=this.y1+this._vy;
}
// constructor
public function Vector(x1:Number,y1:Number,vx:Number,vy:Number):void {
this._x1=x1;
this._y1=y1;
this._vx=vx;
this._vy=vy;
this._x2=this._x1+this._vx;
this._y2=this._y1+this._vy;
this._length=Math.sqrt(this._vx*this._vx+this._vy*this._vy);
this._angle=Math.atan2(this._vy,this._vx);
}
// public functions
public function add(v:Vector):Vector {
return new Vector(this._x1,this._y1,this._vx+v._vx,this._vy+v._vy);
}
public function substract(v:Vector):Vector {
return new Vector(this._x1,this._y1,this._vx-v._vx,this._vy-v._vy);
}
public function dotProduct(v:Vector):Number {
return this._vx*v._vx+this._vy*v._vy;
}
public function project(v:Vector):Vector {
var dp:Number=this._vx*v._vx+this._vy*v._vy;
return new Vector(this._x1,this._y1,dp*this.dx,dp*this.dy);
return new Vector(this._x1,this._y1,dp*this.dx,dp*this.dy);
}
}
}
EDIT: I realize there's some redundancy in there, some stuff that gets repeated that could have been put in functions - like the calculation of the dotproduct when projecting vectors, or recalculating the angle and length - but I figured it would be a tiny bit faster like this, and it's not like these calculations will ever change, right? Unless I made a mistake with them, that is.
Last edited by Fall_X; 08-28-2007 at 06:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|