14 Replies - 661 Views - Last Post: 10 March 2010 - 07:26 PM Rate Topic: **--- 3 Votes

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Working with vectors

Posted 10 March 2010 - 04:17 PM

Im woundering if theres a way to work with a vector of an Image
OR Rectangle?
Take a look a this attachment and get a better picture at what im trying to
achieve.

Attached image(s)

  • Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Working with vectors

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Working with vectors

Posted 10 March 2010 - 04:34 PM

And what do you want to put in your Vector ?
The pixels ? Subimages ?
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Working with vectors

Posted 10 March 2010 - 04:41 PM

@pbl: I think the OP is referring to a mathematical vector (direction and magnitude), not a java.util.Vector.

@Java Student: You can use the repaint() method inherited from Component in combination with a javax.swing.Timer to make animations. Basically, animations come down to modifying an image's position, redrawing it, and repainting. I know if you check out the Java Tutorials section, there are some good animation tutorials. I know both WolfCoder and pbl have especially good tutorials.
Was This Post Helpful? 1
  • +
  • -

#4 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Working with vectors

Posted 10 March 2010 - 04:51 PM

The vector as in if i wanted to do something like...
if (obstacle.topvector collides with sprite.bottomvector) {
// then such and such....
}


@pbl Not that kind of vector =P
@macosxnerd101 Vector as in dir and magnitude, yes =)

Woundering if theres a method in Java to do this sort of thing?
i'v heard of it being done in c++
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Working with vectors

Posted 10 March 2010 - 05:03 PM

You should take a look into using the java.awt.geom package, as it has tools you'll find useful. In glancing through this package, I think the Area class might be what you're looking for (at least in part), b/c it has the getBounds() and intersects() methods.

For more information on the java.awt.geom package, check out the API:
http://72.5.124.55/j...ge-summary.html
Was This Post Helpful? 1
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Working with vectors

Posted 10 March 2010 - 05:13 PM

I found this online....it might be helpful:
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());

    }
}


Vector.java

Remember that Google can be one of your best friends ;)

This post has been edited by zim1985: 10 March 2010 - 05:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Working with vectors

Posted 10 March 2010 - 05:19 PM

In addition, you could actually use a Rectangle to do collision for you. It's actually really easy. If you have a bounding box around each figure (as in the figure and the box, all you have to do is call rectangle.isColliding(otherRect). If that is true, then they collide. That shortness the code quite significantly.

Here is a code snippet that I took from my tutorial:
        // 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;
        }



Here is the full tutorial:
http://www.dreaminco...&showentry=2189

This post has been edited by Dogstopper: 10 March 2010 - 05:19 PM
Reason for edit:: broken tag

Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Working with vectors

Posted 10 March 2010 - 05:19 PM

Wrong type of Vector. The OP is asking about direction and magnitude vector (you may have seen it in physics, Precalc or Calculus BC if you've had those classes), not dynamic array Vector.
Was This Post Helpful? 1
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Working with vectors

Posted 10 March 2010 - 05:26 PM

View Postmacosxnerd101, on 10 March 2010 - 03:19 PM, said:

Wrong type of Vector. The OP is asking about direction and magnitude vector (you may have seen it in physics, Precalc or Calculus BC if you've had those classes), not dynamic array Vector.

That Vector.java thing calculates magnitude and direction and such. I'm in Calculus AB, so I know what a Vector is...that said, I'm pretty sure that's it.
Was This Post Helpful? 2
  • +
  • -

#10 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Working with vectors

Posted 10 March 2010 - 05:28 PM

@zim1985 Umm, that be a over my head a tad but i appreciate your time
@Dogstopper Yeah i used that for a different game but i want to go a little
more indepth and deal with each 4 vectors of a rectangle to explore different
options when making games
@macosxnerd101 java.awt.geom package has alot of interusting methods in it
thanks for pointing that out!

But yes, dealing with each individual vector is what im trying to achieve :smile2:

This post has been edited by Java Student: 10 March 2010 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10648
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Working with vectors

Posted 10 March 2010 - 05:30 PM

My mistake. I saw an array inside the class, and I assumed dynamic array. I re-read and you're absolutely right. :)
Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Working with vectors

Posted 10 March 2010 - 05:42 PM

View Postzim1985, on 10 March 2010 - 07:13 PM, said:

I found this online....it might be helpful:
...

Remember that Google can be one of your best friends ;)


However, also remember that teachers could call it cheating...just beware :devil2:

Quote

@Dogstopper Yeah i used that for a different game but i want to go a little
more indepth and deal with each 4 vectors of a rectangle to explore different
options when making games


What do you mean? How much more? The bounding rectangle method works well! Professional games use fine-tuned bounding rectangles...

This post has been edited by Dogstopper: 10 March 2010 - 05:44 PM

Was This Post Helpful? 1
  • +
  • -

#13 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Working with vectors

Posted 10 March 2010 - 06:16 PM

View PostDogstopper, on 10 March 2010 - 04:42 PM, said:

However, also remember that teachers could call it cheating...just beware :devil2:

No worries im a :angel:
next year (gr.12) is when were allowed to take Computer Science 30AP
It just so happens that today we chose our course selection...so i'v
checked off CompSciAP, i can't wait :D

Dogstopper said:

What do you mean? How much more? The bounding rectangle method works well! Professional games use fine-tuned bounding rectangles...


Say your sprite jumped and you wanted to hit the enemy from the bottom of the enemy's
rectangle, how would the game tell if you hit enemys rect from the left side, right side
or top?

This post has been edited by Java Student: 10 March 2010 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Working with vectors

Posted 10 March 2010 - 06:56 PM

I see, yah, I would go with macosxnerd101's suggestion here and look into java.awt.geom and particularly java.awt.geom.Rectangle2D
Was This Post Helpful? 1
  • +
  • -

#15 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Working with vectors

Posted 10 March 2010 - 07:26 PM

View PostDogstopper, on 10 March 2010 - 05:56 PM, said:

I see, yah, I would go with macosxnerd101's suggestion here and look into java.awt.geom and particularly java.awt.geom.Rectangle2D


Ah ok, i,ll look into Rectangle2D too

This post has been edited by Java Student: 10 March 2010 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1