The Daily Click ::. Forums ::. Klik Coding Help ::. A few questions about Spaceship(asteroids) movement
 

Post Reply  Post Oekaki 
 

Posted By Message

JetpackLover



Registered
  01/03/2007
Points
  212
27th March, 2009 at 07:40:57 -

1. How do you do collision using spaceship movement? As far as I can tell since it depends on which angle it's facing there isn't really a way to stop it from going through a wall or bouncing off of things. I want to make it so when the ship gets shot at it does some kind of knockback to it. I've never used a spaceship type movement before now so it's pretty confusing.

2. If I wanted to add some kind of quick dashing how would I do that? I want to add dashing that dashes Up, Down, Left, Right when you double tap that direction. However I am noticing that will make little sense when you are not facing an upward angle. I hope everyone can understand my dilemma even though I can't explain myself properly.

I will of course try and explain myself in further detail should anyone need it.

By the way I am using this engine http://click.andersriggelsen.dk/examples/files/SpaceEngine.mfa

 
http://www.invincibletime.com/

Devlog for HD MMF Game Omulus. Check it out because it's gonna be awesome. http://omulus.tumblr.com/

Follow me on the twitters https://twitter.com/JetpackLover


Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
27th March, 2009 at 21:33:17 -

That engine is very basic - I did make a more complicated one, but the host I put it on seems to have been discontinued

BASICS
For your ship to move, you use horizontal and vertical velocities.
These are simply added to the ship coordinates each frame.

X_Pos = X_Pos + X_Vel
Y_Pos = Y_Pos + Y_Vel

To make your ship accelerate, you need to change these velocities.

X_Vel = X_Vel + Cos( Angle ) * Thrust
Y_Vel = Y_Vel - Sin( Angle ) * Thrust

Your best bet for the "quick-dash" idea, is just to increase thrust for a short period of time (maybe limit it using a slow-charging energy supply).


DEFLECTION
To make your ship deflected slightly when hit by bullets, you can add a small force, just like you do when the player is holding down the "up" arrow key.
The only difference is that instead of using the angle of the ship, you use the angle of the bullet, and reduce the force.

X_Vel = X_Vel + Cos( Bullet_Angle ) * Bullet_Force
Y_Vel = Y_Vel - Sin( Bullet_Angle ) * Bullet_Force

To add a recoil effect from the ships own guns, you do almost the same thing again.

X_Vel = X_Vel + Cos( Angle + 180 ) * Recoil_Force
Y_Vel = Y_Vel - Sin( Angle + 180 ) * Recoil_Force


BOUNCING
Firstly, your obstacles (walls etc) need to be active objects, not backgrounds. Secondly, if you want the obstacles to bounce as well, it's a lot more complicated, and you will need to use the physics extension instead (good luck with that!).

If you want to make your spaceship bounce of a solid wall, the first thing you need to do is find the angle in which it is moving.

Angle = ATan2( 0 - Y_Vel, X_Vel )
Speed = Sqr(( X_Vel Pow 2 ) + ( Y_Vel Pow 2 ))

Next, you need to find the angle of the surface it is hitting. The best way to do this, is to store the surface angle (normal) in one of the object's alterable values. Calculating the new angle of the ship is then very easy:

Ship_Angle = Wall_Angle - Ship_Angle

Finally, you convert the angle and speed of the ship back to vector format:

X_Vel = Cos( Angle ) * Speed
Y_Vel = 0 - Sin( Angle ) * Speed


SPEEDS
You do something similar if you want to limit your ship's maximum speed:

1.) Angle = ATan2( 0 - Y_Vel, X_Vel )
2.) Speed = Sqr(( X_Vel Pow 2 ) + ( Y_Vel Pow 2 ))
3.) Speed = Min( Speed, Top_Speed )
4.) X_Vel = Cos( Angle ) * Speed
5.) Y_Vel = 0 - Sin( Angle ) * Speed

Again, you can make the ship gradually deccelerate in almost the same way.

 
n/a

JetpackLover



Registered
  01/03/2007
Points
  212
28th March, 2009 at 00:00:37 -

Wow thanks for the indepth look at the spaceship movement Sketchy! I am very interested in the max speed code.

Do you think it is possible if I could have an example that used this (not an MFA just a written line)

MovVecX( "Active" )+Cos(0-(Angle( "Active" )-90))*0.9 (do I add the Speed = Min( Speed, Top_Speed ) to this?
MovVecY( "Active" )+Sin(0-(Angle( "Active" )-90))*0.9 (do I add the Speed = Min( Speed, Top_Speed ) to this?

Or is it something different?

also I was wondering if you could write out the code for changing the ships angle based on the object it hits angle. I find that I learn much faster when I see things how they are supposed to work. Then I can edit the values and a little light will come on in my head that goes "Oh that's how that works. Looking at what you have now I can understand what it does but I am not very clear on where things need to go.

Actually while I was typing this I tried to work it out on my own but I'm unclear about how to use the
ATan2( 0 - Y_Vel, X_Vel ) or the Sqr(( X_Vel Pow 2 ) + ( Y_Vel Pow 2 ))

The only things I was successfully able to do was give the wall an angle in its alterable values, and setup the ships "Angle" alterable value. After that I got quite stumped. As far as I know the first 2 lines is for the condition and then the Ship_Angle = Wall_Angel - Ship_Angle is for the event. I understand what is going to happen and I feel like I can do it but sadly I'm stumped.

 
http://www.invincibletime.com/

Devlog for HD MMF Game Omulus. Check it out because it's gonna be awesome. http://omulus.tumblr.com/

Follow me on the twitters https://twitter.com/JetpackLover


Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
28th March, 2009 at 00:20:38 -

Well, it's possible I made mistakes in what I wrote, as I didn't actually test it.
I will make an example

 
n/a

JetpackLover



Registered
  01/03/2007
Points
  212
28th March, 2009 at 00:32:29 -

Thank you Sketchy!

 
http://www.invincibletime.com/

Devlog for HD MMF Game Omulus. Check it out because it's gonna be awesome. http://omulus.tumblr.com/

Follow me on the twitters https://twitter.com/JetpackLover


Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
28th March, 2009 at 01:29:02 -

First of all, sorry - the formula to calculate the angle after bouncing *was* wrong
The rest should be ok though atleast.

Here's the example file I promised:
http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/Asteroids.mfa

If you use this in a game there are a couple of things you should do to make it less buggy:

1.) Use a circular collision detector for the ship - otherwise it may get stuck in a wall while rotating/bouncing (especially if the ship sprite has wings that stick out a lot or something).

2.) Use the MoveSafely extension (or fastloops) to actually move the ship. Again, it will make the wall collisions much less buggy.

 
n/a

JetpackLover



Registered
  01/03/2007
Points
  212
28th March, 2009 at 02:09:55 -

Thank you for the example Sketchy however it doesn't appear to run on my MMF. It crashes the runtime.

 
http://www.invincibletime.com/

Devlog for HD MMF Game Omulus. Check it out because it's gonna be awesome. http://omulus.tumblr.com/

Follow me on the twitters https://twitter.com/JetpackLover


Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
28th March, 2009 at 02:38:34 -

Have you updated MMF2 lately? Does the version you're using definitely support ATan2? (use non-HWA build)
Suggest you try recreating it yourself from scratch, and see which event(s) cause the crash.

 
n/a

JetpackLover



Registered
  01/03/2007
Points
  212
28th March, 2009 at 06:30:50 -

oh good call Sketchy I'll do that.

 
http://www.invincibletime.com/

Devlog for HD MMF Game Omulus. Check it out because it's gonna be awesome. http://omulus.tumblr.com/

Follow me on the twitters https://twitter.com/JetpackLover


Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
28th March, 2009 at 15:01:50 -

I made some changes so you might want to re-download (nothing that's going to make it work on your computer though).
* embedded collision detection, so ship never gets stuck in wall.
* afterburners (shift + up)
* recoil from ship's own gun (ctrl)

Possible Fix:
Put a "never" condition on all the events using ATan2.
If it turns out that ATan2 is causing the problem, you can use the advanced direction extension instead.

 
n/a
   

Post Reply



 



Advertisement

Worth A Click