OR Rectangle?
Take a look a this attachment and get a better picture at what im trying to
achieve.




3 Votes
Posted 10 March 2010 - 04:17 PM
Posted 10 March 2010 - 04:41 PM
Posted 10 March 2010 - 04:51 PM
if (obstacle.topvector collides with sprite.bottomvector) {
// then such and such....
}
Posted 10 March 2010 - 05:03 PM
Posted 10 March 2010 - 05:13 PM
public class Vector {
private int N; // length of the vector
private double[] data; // array of vector's components
// create the zero vector of length n
public Vector(int n) {
N = n;
data = new double[N];
}
// create a vector from an array
public Vector(double[] d) {
N = d.length;
// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}
// create a vector from either an array or a vararg list
// this constructor uses Java's vararg syntax to support
// a constructor that takes a variable number of arguments, such as
// Vector x = new Vector(1.0, 2.0, 3.0, 4.0);
// Vector y = new Vector(5.0, 2.0, 4.0, 1.0);
/*
public Vector(double... d) {
N = d.length;
// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}
*/
// return the length of the vector
public int length() {
return N;
}
// return the inner product of this Vector a and b
public double dot(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
double sum = 0.0;
for (int i = 0; i < N; i++)
sum = sum + (this.data[i] * that.data[i]);
return sum;
}
// return the Euclidean norm of this Vector
public double magnitude() {
return Math.sqrt(this.dot(this));
}
// return the Euclidean distance between this and that
public double distanceTo(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
return this.minus(that).magnitude();
}
// return this + that
public Vector plus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] + that.data[i];
return c;
}
// return this + that
public Vector minus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] - that.data[i];
return c;
}
// return the corresponding coordinate
public double cartesian(int i) {
return data[i];
}
// create and return a new object whose value is (this * factor)
public Vector times(double factor) {
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = factor * data[i];
return c;
}
// return the corresponding unit vector
public Vector direction() {
if (this.magnitude() == 0.0) throw new RuntimeException("Zero-vector has no direction");
return this.times(1.0 / this.magnitude());
}
// return a string representation of the vector
public String toString() {
String s = "";
for (int i = 0; i < N; i++)
s = s + data[i] + " ";
return s;
}
// test client
public static void main(String[] args) {
double[] xdata = { 1.0, 2.0, 3.0, 4.0 };
double[] ydata = { 5.0, 2.0, 4.0, 1.0 };
Vector x = new Vector(xdata);
Vector y = new Vector(ydata);
System.out.println(" x = " + x);
System.out.println(" y = " + y);
Vector z = x.plus(y);
System.out.println(" z = " + z);
z = z.times(10.0);
System.out.println(" 10z = " + z);
System.out.println(" |x| = " + x.magnitude());
System.out.println(" <x, y> = " + x.dot(y));
System.out.println("dist(x, y) = " + x.distanceTo(y));
System.out.println("dir(x) = " + x.direction());
}
}
This post has been edited by zim1985: 10 March 2010 - 05:13 PM
Posted 10 March 2010 - 05:19 PM
// box is this bounding rectangle, other.getBounds()
// grabs the other rectangle.
// Then it is a simple matter of testing intersections...
public boolean collides(Ball other) {
if (box.intersects(other.getBounds()))
return true;
else
return false;
}
This post has been edited by Dogstopper: 10 March 2010 - 05:19 PM
Reason for edit:: broken tag
Posted 10 March 2010 - 05:19 PM
Posted 10 March 2010 - 05:26 PM
macosxnerd101, on 10 March 2010 - 03:19 PM, said:
Posted 10 March 2010 - 05:28 PM
This post has been edited by Java Student: 10 March 2010 - 05:32 PM
Posted 10 March 2010 - 05:30 PM
Posted 10 March 2010 - 05:42 PM
zim1985, on 10 March 2010 - 07:13 PM, said:
Quote
This post has been edited by Dogstopper: 10 March 2010 - 05:44 PM
Posted 10 March 2010 - 06:16 PM
Dogstopper, on 10 March 2010 - 04:42 PM, said:
Dogstopper said:
This post has been edited by Java Student: 10 March 2010 - 06:17 PM
Posted 10 March 2010 - 06:56 PM
Posted 10 March 2010 - 07:26 PM
Dogstopper, on 10 March 2010 - 05:56 PM, said:
This post has been edited by Java Student: 10 March 2010 - 07:27 PM
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
