Movement using vector maths question. I think.

Previous topic - Next topic

Hatonastick

Well what little respect some of you have for me may just fly out the window after me asking this question, but here goes... :)

I have a player space ship that sits in the middle of the screen.  It doesn't really move, but has a virtual movement speed (that is constant) and can spin around 360 degrees (so it has 'speed' and direction).  Enemy spaceships that come onto the screen also have a speed and direction (and actually move, their speed isn't virtual), but I need them to move in whichever direction they are moving in at a speed that takes into account the players virtual speed and direction ie. a certain amount of pixels per move.

eg. So if the player is moving north and the enemy is moving north (albeit slightly faster) it's going to slowly move up the screen, but if the player is moving south and the enemy is moving north, the distance between them closes very quickly as they are moving directly towards each other.

The illusion of movement comes from the starfield in the background which works fine (I can do starfields in my sleep).  I use this method because it makes adding some other bits that will be in the game much easier than if everything was actually scrolling for real.  Now I did originally have a non-working example of this with some messed up (well it almost worked, just had a bug I couldn't work out) formula of my own, but I no longer have Game Maker and in any case seem to have lost the source.

So...  Does anyone have a formula that might actually work?  I've had a look at some pages on vector maths but suspect I'm just going to make the same mistakes I made last time and that had me stumped.

Edit: Ok I've gotten some of the way with the maths, but I'm not 100% sure it is right.  Well, some of it is just not so sure I'm taking it to the right logical conclusion so I'm going to try and whack together something to test it out.

Edit:  Woo hoo!  I solved it.  Don't freaking believe it.  My maths skills are rustier than rust, but I muddled my way through understanding/working out/remembering this base level high school maths.  At least it seems to work fine in my test.  Certainly doesn't do the bizarre behaviour I remember my old test in Game Maker doing.  This one actually looks right.  Yay! :blink:

Edit: Nope... Still not right.  Ok, the first bit is, the bit where I get the x and y component of the player an enemy vectors.  What is messing up is the next bit.

Edit:  Solved.  Well I sat down with some pieces of paper and a calculator and went through it all step, by logical step.  In the end it was all pretty simple (at least the formula is), what was causing the problem was two gotcha's.  Firstly (on a 2D plane) the coordinate system my formula was working on has y+ as north, while the screen coordinate system has y+ as being south.  Secondly I had to take into account that while the player and enemy are moving in the game world, on screen the player is static in the center and only the enemy moves.  Once I got around those two it came together pretty quickly and now it all works.  Or at least it seems to.  I haven't tested all situations yet in game, but on paper I did.  You math gurus would have gotten this in seconds flat.  I'm really not terribly good at anything beyond basic arithmetic and basic use of SIN, COS etc. :)
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Kitty Hello

Uhm. Yes. And if you have other questions, just ask. :D

Hatonastick

Well I figured either I'd have to do some complex diagram to try and explain what I was doing because my explanation reads like gibberish, or just plod along until I figured it out myself.  Option B (thankfully!) became the easier of the two. :)
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

MrTAToad

There were a few games on the Archimedes that did this - Revolver was a pseudo 3D game and another with a spaceship that did this sort of thing - its very clever.

Hatonastick

I never got to use that computer system but wanted to because we didn't get any of them in my neck of the woods.  Only Archimedes game I remember was Zarch, which I played on my Atari ST where it was known as Virus.

Just had a look at Revolver.  Amazing how many mini-videos are on YouTube of old games in motion.  Very handy reference while looking for old games you aren't familiar with that might be worth researching and remaking.

Anyway my game isn't going to be as smart as Revolver I'm afraid. :)  Actually it's not going to look very smart at all.  Just an ordinary shooter.
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Slydog

Was your solution as easy as this?:

1. Calculate the player's movement vector (x and y change for this frame).
2. Calculate each enemy's movement vector (independent of the player).
3. Update each enemy's position (relative to the player's fixed (0,0) position) by adding the vector difference of the enemy movement vector and the player's current movement vector.
4. Adjust all positions so that (0,0) is the center of the screen. (or just always use the actual center pixel location in the above calculations)
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Hatonastick

#6
Yes.  No.  Maybe. :)

Every time I read your explanation I seem to have a different idea as to what you have suggested -- not because yours isn't clear, but because the explanation I gave in my source code is rather garbled.  So sometimes I think yes, other times I think well no.  :S

What I did end up with is 4 lines of code, 2 for x and 2 for y.

Where:
ps = player speed
es = enemy speed
pd = player direction (angle in degrees)
ed = enemy direction (angle in degrees)
ex, ey = the enemy's new location on screen

Code (glbasic) Select
nx = 0 - ((ps * COS(pd)) - (es * COS(ed)))
ny = (ps * SIN(pd)) - (es * SIN(ed))
ex = ex + nx
ey = ey + ny


This allows me to draw the enemy in relation to the player, depending upon the facing and speed of both, while taking into account that the player is only pretending to move.  There's probably more optimisation that can be done, I don't really care.  I just care that it works.

Edit:  I've had to edit my reply half a dozen times now because my reading comprehension seems to have abandoned me.  Either that or my notes are the garbled product of someone with a lot of fatigue.  That and my maths skills aren't really up to much.  :blink:
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Hatonastick

#7
Gernot and Ian, I must have started this for some Retro Remakes competition or something.  Not really sure.  I just found this comment at the top of the source:

This isn't really a game for Bob.  No, this is more for the 'Driving Miss Daisy' shoot-em-up set.  People who want a shoot-em-up that plays like a leisurely drive through the countryside, as opposed to a manic, twitchy, dodge-5000-bullets-of-hell-on-screen-all-while-screaming-as-your-eyes-and-ears-bleed-from-the-graphical-and-audio-assault style game.  No, this is a game for people like me.

:blink:
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

MrTAToad

Quote from: Hatonastick on 2010-Jul-12
I never got to use that computer system but wanted to because we didn't get any of them in my neck of the woods.  Only Archimedes game I remember was Zarch, which I played on my Atari ST where it was known as Virus.

Just had a look at Revolver.  Amazing how many mini-videos are on YouTube of old games in motion.  Very handy reference while looking for old games you aren't familiar with that might be worth researching and remaking.
The Archimedes was very nice - most commercial applications were in BASIC because it ran so fast...  Wish I could record my Walker game (wot I wrote), but unfortunately Fraps doesn't work with Red Squirrel...

Kitty Hello

Wow, that revolver thing looks amazing. Very clever idea to render the game on a sphere.

My idea would be: Because you have to render each frame to an offscreen sprite anyway, I'd do the movement/translation on that sprite as I would code the scrolling of a map. Then I'd statically draw a sphere with the texture applied.
You "might" apply the y-rotation of the sphere if you need that effect, too.

I never got hands on an archimedes, but that was always my dream in early 90s. I heard they even wrote that "desktop" program in BASIC. A-Ma-Zing!


Hatonastick

Quote from: MrTAToad on 2010-Jul-13
The Archimedes was very nice - most commercial applications were in BASIC because it ran so fast...  Wish I could record my Walker game (wot I wrote), but unfortunately Fraps doesn't work with Red Squirrel...
Sounds pretty cool to me. :)  Could always convert your game to GLB maybe?

BTW for those who are interested in seeing the formula above in action, here's a simple example.  Left/right cursor keys to move, S to toggle starfield.  Keyboard input is a bit twitchy, so you may have to whack the S key quickly to get it to toggle properly.  This is all very alpha WIP stuff...

[attachment deleted by admin]
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Hatonastick

Quote from: Kitty Hello on 2010-Jul-13
Wow, that revolver thing looks amazing. Very clever idea to render the game on a sphere.

My idea would be: Because you have to render each frame to an offscreen sprite anyway, I'd do the movement/translation on that sprite as I would code the scrolling of a map. Then I'd statically draw a sphere with the texture applied.
You "might" apply the y-rotation of the sphere if you need that effect, too.
Funnily enough I'd thought of trying that out too after seeing Revolver in action - mostly though because it reminded me of my favourite Blitz 3D demo 'Insectoids' (see attachment on this message for a compiled version).

Quote from: Kitty Hello on 2010-Jul-13
I never got hands on an archimedes, but that was always my dream in early 90s. I heard they even wrote that "desktop" program in BASIC. A-Ma-Zing!
Same.  They just didn't exist in my neck of the woods (unless someone specially imported it).  It was pretty much all Amiga's and Atari ST's here.  Our schools back in the early 80's had heaps of the earlier model Acorn (BBC Micro) 8-bit computers which I really liked because I enjoyed programming in BBC Basic, but for some reason though we never got to see the Archimedes.

[attachment deleted by admin]
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

Schranz0r

I start insectoids, and the first thing i got are MAV...
Thats why i love BB!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Ian Price

I got the same (MOV). BB was/is a total bastard at times. That's one of the reasons I switched languages - eventually to GLB. Gotta love BB! :P

I do however remember Insectoids from the first time around - though I only played it briefly back then, as I thought it was crap. Sorry HOAS!
I came. I saw. I played.

MrTAToad

#14
QuoteCould always convert your game to GLB maybe?
Not good enough for GLBasic :)  Plus, I dont like designing levels...  Pretty good example of rotation!