Jump to content

Vector or dir+vel?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
19 replies to this topic

#1
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
I've decided to write a "particle" class, as a learning project and for fun, to be able to create simple "rule driven" particle collision "simulators" to try out what happens with different collision rules and such.

The only thing is..I got stuck at the starting line.

The matter is simple: Should my "particles" have a vector describing both speed and direction, or is it wiser to go with a direction and velocity approach? I'm talking purely 2D here, and collision detection and deflection calculation seems to be the primary tasks. Although, that's some math I will have to learn when I write the actual application.

What do you think is the best approach? I.e what makes later calculations, in the actual application, easier?
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
direction is already a vector with length 1. velocity = direction * speed.

I think a simple velocity vector would be simpler.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
Oh, I was thinking more like having a variable describing direction in the form of a 360-degree direction, and a another one describing speed. I think I'm confusing everything by using the incorrect terms.

But maybe a velocity vector is a good thing. I guess I have to define the type myself? Or is there any built-in 2-d velocity vector format in any of the "standard" includes?
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#4
bsteeg

bsteeg

    Newbie

  • Members
  • Pip
  • 3 posts
It's best to build them for yourself, thats where the learning starts ;)

#5
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
True. I found out that there is a vector-class in STL, but it seemed overly complicated for my needs :) I guess in theory a simple int[1] would do.

Maybe even better:

typedef struct{

int x;

int y;

} vector;

Edited by Walle, 06 January 2010 - 09:36 AM.
Added some code

________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The vector class has nothing to do with the type of vectors you're thinking of.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
I think I figured that out after looking at it a bit more. It seems to have to do with linked lists, am I correct?

Anyway, my struct should work for what I'm doing (even though I'm putting it aside for a few days), right?
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A linked list would probably work well to store your particles, yes.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
bsteeg

bsteeg

    Newbie

  • Members
  • Pip
  • 3 posts

WingedPanther said:

A linked list would probably work well to store your particles, yes.

Array can do the trick to ;) and it's a easier way out than a linked list.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
For a particle system where they may be getting created and destroyed frequently, an array would NOT be a good idea.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Feral

Feral

    Programmer

  • Members
  • PipPipPipPip
  • 162 posts
create a struct or class with x, y, a speed variable and a bool for if its alive or dead. then create an stl vector or list and fill it with your structure.

Iterate over the container that you choose updating the position, speed and life of the particle.

The way that a particle system works you are constantly adding and removing particles so an array won't work as WingedPanther said.

#12
kmhosny

kmhosny

    Programmer

  • Members
  • PipPipPipPip
  • 133 posts
i think each particle wont need to hold a vector of speed and distance, it would only be variable for speed and a variable for distance, since a class called Particle refers to a single particle and if your application will have many "Particles" then your main program will hold a vector of "Particle class" with each element in the vector refers to a single particle.