Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / A question about vectors/trig and asteroid style ship movement

Author
Message
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 16th May 2010 18:38
Hi Guys,

This feels a bit of a n00by question, so I'm posting it here.

I've been puzzling over this for a while now, but just keep running into deadends, with little or no joy or success.

I'm currently doing a conversion of an old Spectrum game (Which I thought would be nice and simple to do... lol), in this you control your little player using something similar to the control's you would Expect in a game of Asteroids, only with 2 minor differences. 1) You can reverse, and 2) There is air resistance (i.e. your ship slows and stops eventually, if you don't keep thrusting)



Here is my best working code to date, altho it's pretty close to what I want, it suffers from issues when bouncing off walls and then thrusting.

I've tried adapting the vector asteroids tutorial code to work, and I've tried just using basic trig; but to no avail Searching hasn't helped in this case.

Can someone please point me in the right direction?

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 16th May 2010 19:38
The simplest method to simulate inertia (moving in the same direction while pointing in another) is to create a dummy object and use it for navigation.

Make a small cube and hide it, then when the player turns it only turns the ship, not the cube. When the player thrusts you slowly turn the cube to face the same direction as the ship. If they stop thrusting, then you stop turning the cube.

Bouncing off of walls is as easy as calculating the angle the cube was at, at the time of impact, and then calculate the resulting angle of travel after the impact. You only turn the cube, not the ship.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 16th May 2010 19:49
Thank you KISTech, I'll give it a go

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 16th May 2010 21:23
It's not so difficult if you know some basic stuff.

Some very basic Newtonian movement would look like:

vel = vel + acc
vel = vel * drag
pos = pos + vel

Where fields in bold are vectors, and drag is a value between 0 and 1 (where 1 is no drag at all, and 0 is no movement).

As long as you keep those equations running, all you have to do is control the acceleration.

To do that, you just need an acceleration value and an angle.

acc.x = sin(angle) * value
acc.y = cos(angle) * value

Where value is 0 if you're not pressing any buttons, negative if you're reversing and positive if you're moving forward.

"everyone forgets a semi-colon sometimes." - Phaelax
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 17th May 2010 08:10 Edited at: 17th May 2010 08:15
I agree with zotoaster. Vector addition and forces should do just fine.

[edit]
after realizing how unhelpful my post is

keep a vector containing how fast the object is moving in what direction (three values x, y, and z, the length of this vector is it's overall velocity) and keep adding that to the position of the camera. When the user clicks (or decides to move, however), move the camera or a dummy object forward from the camera, grab that position, and subtract that position from the original position to get your acceleration direction, and multiply that by the desired acceleration, then add that to the velocity vector. GOOD to go!


=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 17th May 2010 11:37
Thank you guys.

As soon as I get a spare 5 mins I will try out both methods and see which one I get on best with/have most success doing.

Thanks again, and I'll post back if I have any more issues with it

Spectre 117
14
Years of Service
User Offline
Joined: 18th Apr 2010
Location: *Insert cheesy location here*
Posted: 23rd May 2010 05:19 Edited at: 23rd May 2010 05:23
Prof,
I had the same issue a few weeks ago and Serial Velocity helped me out with a code snippet for the dummy object method. I can speak for experience when I say the it is the simplest method to get the desired effect you are look for and its not nearly as calculation intense as programming with trig. You can find the code here http://forum.thegamecreators.com/?m=forum_view&t=169703&b=7
hope it helps. Also and drag to this code won't be difficult either.
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 23rd May 2010 12:49
Thanks Spectre 117, I've looked at the snippet and it does look helpful.

I've as yet had no time to work any further on this due to other commitments, but I will soon.

Thanks again to everyone who has helped me on this.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 24th May 2010 11:47 Edited at: 24th May 2010 11:54
Sorry for the double post, but I did it

I used vectors in the end, as suggested by Zotoaster;



The collisions caused me a little bit of grief, but I got that working about 1am last night, lol.

Now all I need to do is integrate some Timer Based Movement, (Never done TBM with vectors before) and "Gravity Zones" (Where your ship is pulled toward a specific object) then I'm a very happy bunny

Thanks to everyone who's helped me with this

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 25th May 2010 11:55
Gah! I'm not having any luck trying to integrate Timer Based movement into the above snippet.

Its working with the Steering ok, but I have little understanding of how vectors work (This is my first time using them), or how to use TBM to scale the speed and acceleration/deceleration.

Please can someone give me a gentle nudge in the right direction?

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 25th May 2010 13:50 Edited at: 26th May 2010 10:37
Well, if you're trying to use Euler's method:

m*a = F - friction * v

a(n+1) = F/m - friction * v(n)
v(n+1) = v(n) + a(n+1) * dt
x(n+1) = x(n) + v(n+1) * dt

or to get to Zotoasters lines:
drag = 1 - friction, so
a(n+1) = F/m
v(n+1) = v(n) - friction * v(n) + a(n+1) * dt
= (1 - friction) * v(n) + a(n+1) * dt
= drag * v(n) + a(n+1) * dt
x(n+1) = x(n) + v(n+1) * dt
[EDIT] I made a mistake here. See next post. The following should be still the same though.

dt is the time that is elapsed, and can be determined using the following at the start of the loop


(dt = Elapsed#)

So basically you multiply the acceleration and speed vector with Elapsed# just before you add them to the other vector (speed and position).

You might have to alter the speeds a bit in order to get the effect you want. Because when you multiply with Elapsed# as calculated above, the acceleration will be in units/secondĀ² and speed in units/second.

If you don't want to alter any values, then use this as the time factor dt:


If I made a mistake somewhere along the way do tell.

Cheers!
Sven B

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 26th May 2010 10:35
Hi =Prof=

I realized there was a mistake in my formulas. The first one was right, but if you want to use timer based movement with friction then they are wrong when I try to get to Zotoasters version.

Timer based movement with friction works a little different. Friction is actually a force, so you'll get
a = F/m - friction * v
v = v + a * dt
x = x + v * dt

Sorry for the inconvenience.

Sven B

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 26th May 2010 11:27
@SvenB
No worries I did try getting it work, but had no joy. Will try with your corrected stuff this evening.

I dunno why I'm struggling so much with this, I have an A-Level in Applied maths, and another in Physics, but somehow Vector maths (and it's application in games) just really confuses me.

Will post back with my successes/failures

Thanks again.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 15th Aug 2010 17:18 Edited at: 15th Aug 2010 17:27
@SvenB:
I still haven't been able to get this to work properly.

I have to admit, I did get fed up and leave it alone for a little while and worked on other things.

But have been trying again the last few days but to no avail.



The code above works as expected; with the ship accelerating when thrust is applied, and conitinuing uninterupted until you thrust again.

However, when you uncomment the 2 lines where the Timer Based Movement factor is applied to the vectors (And change the value for acceleration); then the ship stops when your not thrusting.

I've removed all the collision and drag stuff for the time being until I can get this issue sorted. (Initially, I thought I was doing the drag wrong, and wasted many hours trying to rectify that)

Can you (or anyone else) see where have I gone wrong?

Thanks as always

=PRoF=

>edit<
Removed some stuff from the code from when I was trying with hidden objects instead of vectors

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 15th Aug 2010 19:11 Edited at: 16th Aug 2010 13:00
Hi =Prof=,

The speed vector has to be multiplied with the time factor when it is added to the position vector, but by using multiply vector, the speed vector is changed. Therefor you're not storing the speed vector, but the speed vector multiplied by the time factor.
There are a number of solutions for this, the easiest one being writing the formula per component:



Cheers!
Sven B

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 15th Aug 2010 22:52
Thanks so much SvenB

It's all sorted now and working 100%

Once again the collisions proved troublesome, but I've done it now

Thanks again

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 17th Aug 2010 10:31
Quote: "but somehow Vector maths (and it's application in games) just really confuses me."

search "vectors don't bite!" on the forums to get an amazing tutorial.

What really makes vectors amazing in my opinion is the dot product, because, where A is a normalized vector and B is any old vector, A*B (A.x*B.x+A.y+B.y) literally gives "the magnitude of B in the direction of A", or ||A||*cos(theta) where theta is the angle separating the two vectors. There's some really cool stuff in that tutorial.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?

Login to post a reply

Server time is: 2024-09-28 20:32:46
Your offset time is: 2024-09-28 20:32:46