playMaker

Author Topic: Add Force 2D and Set Velocity 2D - Local rather than World Space  (Read 2002 times)

OliverAge24

  • Playmaker Newbie
  • *
  • Posts: 8
I've tried absolutely everything I can possibly think of but I can't figure out how to add force/set velocity using 2D physics... relative to an objects local rotation.

So for example... if you were making an asteroids clone... Whenever you press the "up" key, you want to add force to the spaceship, in the direction it is facing...

I might be being a moron, but using the actions in the 2D package... I can't figure out how to do this at all.

I've tried editing the actions to include the ".up" transform thing... but I think I'm doing that wrong as I get loads of errors lol.

Many thanks,
Oliver :-[

OliverAge24

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Add Force 2D and Set Velocity 2D - Local rather than World Space
« Reply #1 on: April 16, 2015, 04:35:05 AM »
I came up with a workaround and feel like some sort of rain man right now...

First you get the rotation of the z axis. Which is a number that extends beyond -360 and 360... so you can pass 360 and get to, say, 380, which is the same as 20.

So, first of all you check if it's higher than 360, if it is, you subtract 360. Then you check if it's less than 0... and if it is, you add 360.

Now we've got a value between 0 and 360.

What we want to do now, is split it into sections, 0-90, 90-180, 180-270, 270-360.

So we do a check if it's between 0 and 90, inclusive of 0 and 90.

If it is... we divide it by 90.... now it's a value between 0 and 1. we multiply the FORCE we wanted to add... by this... to give us our Y value of Force to add. We also subtract our value from 1, and then multiply this against the FORCE again, to give us our X value.

For 0 rotation, this puts all of the force into the Y axis. For 90, it puts all of the force into the X axis.... which means as you rotate between 0 and 90, applying force, it will divide it so that you always apply force relative to the rotation.

if the value wasn't between 0-90 you check if it's between 90 and 180, then it's the reverse of the 0-90... you multiply it by the Force to give you the X axis... and subtract it from 1 and then multiply by the force to give you the Y axis.

You then do the same related to 180-270 and 270-360... and you can add force related to the characters rotation!

It's probably the most long-winded way of doing this... but everything else didn't seem to do what I was trying to do, and I'm potentially a huge idiot... but who knows. lol