Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  vector formulas  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline rikqt

Senior Newbie





« Posted 2014-03-26 12:28:17 »

I have to write a 2D Vector class in Java,  that supports the operation of the vector addition subtraction, scalar multiplication, and division and taking the length and unit vector, where each method returns a new Vector.
I would be grateful if someone could provide a resource link or write this formulas.
Online trollwarrior1
« Reply #1 - Posted 2014-03-26 12:35:08 »

Hm.. I might be wrong, but the only thing I don't know is scalar.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public class vec2 {

   public float x, y;

   public vec2(float x, float y) {
      this.x = x;
      this.y = y;
   }

   public int getX() {
      return (int) x;
   }

   public int getY() {
      return (int) y;
   }

   public vec2 multiply(float mult) {
      return new vec2(x * mult, y * mult);
   }

   public vec2 devide(float dev) {
      return new vec2(x / dev, y / dev);
   }

   public vec2 multiply(vec2 vec) {
      return new vec2(x * vec.x, y * vec.y);
   }

   public vec2 devide(vec2 vec) {
      return new vec2(x / vec.x, y / vec.y);
   }

}
Offline Danny02
« Reply #2 - Posted 2014-03-26 12:36:01 »

No, pls don't provide him with resources. Not that there aren't any vector classes just a single mouse click away on the internez. This such a trivial "problem" that you should just write them on you own, it will cost you 5min of your life.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rikqt

Senior Newbie





« Reply #3 - Posted 2014-03-26 12:39:19 »

I knew formulas for those Smiley, but im not sure how to write for a unit vector ?

I think this is formula for scalar multiplication
1  
2  
3  
4  
5  
6  
public Vector2D scalarMulti(Vector2D vector, double scalar) {
        return new Vector2D(
                vector.x * scalar,
                vector.y * scalar
        );
    }
Offline Danny02
« Reply #4 - Posted 2014-03-26 12:43:16 »

here have spoon full of delicious wikipedia
Online trollwarrior1
« Reply #5 - Posted 2014-03-26 12:47:37 »

So unit vector is vector with length of 1?
Offline actual

JGO Coder


Medals: 23



« Reply #6 - Posted 2014-03-26 12:50:49 »

Yup.
Offline rikqt

Senior Newbie





« Reply #7 - Posted 2014-03-26 13:07:08 »

What do u mean by that ? Should I multiply with 1 ?
Offline saucymeatman
« Reply #8 - Posted 2014-03-26 13:16:28 »

Try this :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
//in Vector2
public void add(Vector2 addWith) {
boolean hasTriedWikipidea = false;
boolean hasTriedGoogle = false;
while(true) {
if (!hasTriedWikipidea)
     tryWikipidea();
else if (!hasTriedGoogle)
     tryGoogle();
else
     break;
}
tryJGO();
}
Online trollwarrior1
« Reply #9 - Posted 2014-03-26 13:16:49 »



Basically you to pithagorean theory and you get 1.

So for red one: 0*0+1*1=1;
Green one: 1*1 + 0*0 = 1;
Blue: 0.7*0.7+0.7+0.7=1; (Not really 0.7, sin(45)=0.707(....))
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rikqt

Senior Newbie





« Reply #10 - Posted 2014-03-26 13:19:54 »

Basically you to pithagorean theory and you get 1.

So for red one: 0*0+1*1=1;
Green one: 1*1 + 0*0 = 1;
Blue: 0.7*0.7+0.7+0.7=1; (Not really 0.7, sin(45)=0.707(....))


So my method should be boolean and check if it is positive or what ?
Online trollwarrior1
« Reply #11 - Posted 2014-03-26 13:25:34 »

No idea. You just said something about unit vectors, you didn't mention what you need to do.
Offline rikqt

Senior Newbie





« Reply #12 - Posted 2014-03-26 13:28:23 »

No idea. You just said something about unit vectors, you didn't mention what you need to do.

I didn't understand also what they mean  by that,
Quote
2D Vector class in Java,  that supports the operation of the vector addition subtraction, scalar multiplication, and division and taking the length and unit vector
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2014-03-26 13:30:57 »

'Making the unit vector' is also called normalization that might help you with google-ing Wink

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline rikqt

Senior Newbie





« Reply #14 - Posted 2014-03-26 13:33:47 »

'Making the unit vector' is also called normalization that might help you with google-ing Wink

I've seen many formulas, I even rememeber one like this for example:

1  
 length = sqrt((ax * ax) + (ay * ay) + (az * az)) 


but im not sure if it is something like this for 2D:

1  
sqrt(x * x + y * y)
Online trollwarrior1
« Reply #15 - Posted 2014-03-26 13:35:31 »

Maybe normalize? What that does is take any vector and convert it to length 1 vector.

I had found a good article about what it means, but can't find it, so gonna write it here.

To normalize a vector, first find the length of current vector.

1  
float length = sqrt(vec.x*vec.x + vec.y*vec.y);


Now that you have length of the vector, divide each vector component by the length.

1  
vec2 newvec = new vec2(vec.x/length, vec.y/length);


If im not mistaking, you would get a vec of length 1.
Offline rikqt

Senior Newbie





« Reply #16 - Posted 2014-03-26 13:37:14 »

That's it, thanks!
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (35 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (32 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (35 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!