Jump to content


- - - - -

Move in Any 2D direction. (Server)


8 replies to this topic

#1 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 25 January 2013 - 05:14 AM

UP, DOWN, LEFT, RIGHT.. is pretty straight forward with X/Y coordinates.. Reduce, increase them..

But what If I do not want restricted movement like that? I would like to move a spaceship into any chosen 2D direction.


On a Grid I would do something like this:

(pseudo code)

Grid_Array = [
[0,0,0],
[0,0,0],
[0,0,0]
]

If move request is "UP" {

Each Second:
player = Grid_Array[player.y - 1][player.x]

}

But How would I do this without a grid, and into every direction?

Every Second:
Move player X,Y at 360 angle.


Haeelp!
(It's something with sin/cos isnt it?

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 25 January 2013 - 05:38 AM

You want to use vectors! :) Read that blog post and the next two in the series, to get a good start.

Then, all you need to know is that the formula for a unit vector pointing in a certain direction is just (x = cos(angle), y = sin(angle))

(BTW, congrats - you're one of today's lucky 10,000 on vectors! Well, maybe perhaps a bit less than 10,000, since vectors probably aren't something everyone knows by the time they're 30...)
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 25 January 2013 - 07:14 AM

Cheers :)

Ironically, I have just won 10 dollars from a water bottle. There are 10.000 of those 10 dollar prizes. Awesome coincidence!

#4 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1616 posts

Posted 25 January 2013 - 09:37 AM

I don't think vectors are one of those things everyone knows when they are 30.
Currently using Blender and Unity.

#5 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 25 January 2013 - 10:42 AM

View Postfireside, on 25 January 2013 - 09:37 AM, said:

I don't think vectors are one of those things everyone knows when they are 30.

Whew! Good thing that I got a few years then.

#6 TheNut

    Senior Member

  • Moderators
  • 1718 posts
  • LocationCyberspace

Posted 25 January 2013 - 12:23 PM

fireside said:

I don't think vectors are one of those things everyone knows when they are 30.
Exactly, some don't even know what a vector is their entire life :D I don't blame them though, math is difficult to communicate and I suppose you can say that it takes a while to absorb, depending how willing you are to try.
http://www.nutty.ca - Being a nut has its advantages.

#7 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 26 January 2013 - 01:37 AM

View PostTheNut, on 25 January 2013 - 12:23 PM, said:

Exactly, some don't even know what a vector is their entire life :D I don't blame them though, math is difficult to communicate and I suppose you can say that it takes a while to absorb, depending how willing you are to try.

I was (am) sure he was being very sarcastic. Or at least I woulndt see why I would stumble upon vectors in my thirties. Maybe midlife crisis has to be remedied with them, who knows.
I knew a little about vectors because I do some 3D modeling, but this was unheard of :)

The article is GREAT! Very easy to understand! And very educational (I always wondered how I would do the Mario Jump!)

Thanks again
Reedbeta!

#8 fireside

    Senior Member

  • Members
  • PipPipPipPip
  • 1616 posts

Posted 26 January 2013 - 05:08 AM

I wasn't being sarcastic, I just think the odds that were used in the xkcd cartoon that was linked wouldn't apply in this situation. You might be the lucky one thousand or something, since the average person doesn't understand vectors by the time he/she reaches adulthood. At least, in my experience, I haven't found that many that understand it unless they are into math. Anyway, the sentiment was correct. It's always nice to learn something new.
Currently using Blender and Unity.

#9 Schoening

    Member

  • Members
  • PipPip
  • 17 posts

Posted 26 January 2013 - 08:14 AM

Yes, most of the time that is nice.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users