Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Vectors!  (Read 1961 times) 0 Members and 1 Guest are viewing this topic.
samcp12

Junior Devvie

Medals: 1

 « Posted 2013-09-03 05:01:13 »

Hello, I've been learning how to implement vectors into game programming recently but I'm having some trouble with a couple of things. Mainly if I want an object to go from one point to another, would I create a vector where the components are as followed

V = <sprite1.x - sprite2.x, sprite1.y - sprite.y>

and then normalize, and then add the resulting x and y values onto a sprites x and y?

And would this be a Velocity vector since it's changing the sprites position every game cycle?

DrHalfway
 « Reply #1 - Posted 2013-09-03 05:50:31 »

Velocity is a vector which has both "direction" and "speed"

You find the direction by normalizing the Velocity vector, and the speed is the magnitude or the length of the vector.

If you have a 2D sprite at position V1 = <x1, y1> and want to move it a 2D position with vector V2 = <x2, y2> you can find the velocity vector for movement by doing the following

velocity = V2 - V1

float speed = velocity.magnitude
Vector direction = velocity.normalize

I hope that explains things a bit =]

Danny02
 « Reply #2 - Posted 2013-09-03 06:03:49 »

Using vectors for your game is totaly easy, you don't need to think about such "complex" stuff as velocity vector.

Lets say we have to objects P and Q and you want P to move to Q

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `//you have the "position" vectors of P and Q this means you now the coordinate difference between O(origin) and P,QVector OP = ...Vector OQ = ...//now to get from P to Q you need to now the difference between the two coordinats//to get from P to OVector PO = -OP//and the to go to QVector PQ = PO + OQ//when you now want to only move 2 units in one gamestep towards Q you do this//first normalize PQ, after that the length is 1Vector nPQ = normalize(PQ)//then just multiply with the amount you want to move in one stepVector movement = nPQ * 2//to get your new "position" vector after this step just add it to your current positionVector newP = P + movement`
samcp12

Junior Devvie

Medals: 1

 « Reply #3 - Posted 2013-09-03 12:57:34 »

Thanks but that was pretty much what I said, I was adding the resultant direction vector components (from the vector subtraction of the two points) to the sprite that I wanted to move, I mean I tested it before and it worked I just wanted some clarification that I was going the right way about things.
quew8

JGO Knight

Medals: 53

 « Reply #4 - Posted 2013-09-03 15:59:54 »

Yes you are.

If you wish to know more about working with vectors then I suggest this article: http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/. That is by far my most linked link. It's written by a game developer, with other game developers in mind and it's a great tutorial IMO. Also there is matrix and some transform stuff later on, which is also extremely useful once you get to that point.

Also @Danny02: I'm not sure what you're trying to say. Java doesn't have operation overloads.
samcp12

Junior Devvie

Medals: 1

 « Reply #5 - Posted 2013-09-03 16:02:21 »

Yeah I just wanted to make sure, and I find vectors really interesting so I'll be sure to check that article out, thanks!
jonjava
 « Reply #6 - Posted 2013-09-03 16:24:46 »

Vectors aren't magic sticks that do things automatically - they're just a box with 2 values in them.

How you decide to perceive and use those 2 values makes them "different".

There already is a thread on Vectors - use the Search function next time.

http://www.java-gaming.org/topics/implementing-vector-maths/30102/msg/277581/view.html#msg277581

For example, instead of moving an object like this:

 1  2  3  4  5  6  7  8  9 `int x = 0;int y = 0;int xspeed = 1;int yspeed = 2;step() {  x += xspeed;  y += yspeed;}`

With Vectors you could do it like this:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 `Vector position = new Vector(0,0);Vector speed = new Vector(1,2);step() {  position.addVector(speed);}class Vector {int value1 = 0;int value2 = 0;Vector addVector(Vector v) {  this.value1 += v.value1;  this.value2 += v.value2;  return this;}`

You do the same thing basically where Vectors are just a useful abstraction.

Several Kilo-Bytes

Senior Devvie

Medals: 11

 « Reply #7 - Posted 2013-09-03 21:36:28 »

The answer to the first post is "yes." I don't know what question other people are answering. Do learn as much vector math as you can (including matrices.) Vectors are a type of number in the same way counting numbers, rational numbers, Booleans, and imaginary numbers are. Pursue an understanding of the mathematical operations; The source code implementation is trivial.
samcp12

Junior Devvie

Medals: 1

 « Reply #8 - Posted 2013-09-04 02:51:07 »

Lol, that made me chuckle, and that is a pretty cool way of implementing them, thanks for the help
Pages: [1]
 ignore  |  Print

 EgonOlsen (42 views) 2018-06-10 19:43:48 EgonOlsen (22 views) 2018-06-10 19:43:44 EgonOlsen (43 views) 2018-06-10 19:43:20 DesertCoockie (198 views) 2018-05-13 18:23:11 nelsongames (124 views) 2018-04-24 18:15:36 nelsongames (123 views) 2018-04-24 18:14:32 ivj94 (864 views) 2018-03-24 14:47:39 ivj94 (125 views) 2018-03-24 14:46:31 ivj94 (768 views) 2018-03-24 14:43:53 Solater (140 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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