Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Vectors!  (Read 647 times)
0 Members and 1 Guest are viewing this topic.
Offline 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?

Thanks in advance!  Wink
Offline 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 =]

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

lets start with you having a some fully functional vector classes.

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,Q
Vector 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 O
Vector PO = -OP
//and the to go to Q
Vector 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 1
Vector nPQ = normalize(PQ)
//then just multiply with the amount you want to move in one step
Vector movement = nPQ * 2

//to get your new "position" vector after this step just add it to your current position
Vector newP = P + movement
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline quew8

JGO Coder


Medals: 33



« 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.
Offline 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!
Offline 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.

Offline 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.
Offline 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 Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!