playMaker

Author Topic: Vector 2 from Z rotation and magnitude[SOLVED]  (Read 4990 times)

nick_h82

  • Playmaker Newbie
  • *
  • Posts: 36
Vector 2 from Z rotation and magnitude[SOLVED]
« on: December 04, 2013, 02:30:22 AM »
I'm currently updating a project from 3d physics to the new Unity 2D system. Thanks Jean for the excellent new action set! I have all the basic interactions correct, however I can't quite wrap my head around the final stage.

I'd like to store a Vector2 derived from an angle along the z axis, along with a magnitude. I can get the rotation, and the magnitude would be a float variable, but I can't work out the translation of these to a vector2. I don't need to change the rotation and magnitude once set, instead I would blend between vector2s as the player moves around the scene (would be interested to discuss the best way of doing this too).

I'm pretty sure the Trigonometry and Vector2 action sets would contain everything I need, I just couldn't seem to apply the maths.

Edit, thought I'd expand on the blending aspect. Think of moving through a scene, with different regions of wind defined by direction, and magnitude. As the player moves in and out of these different triggers, the vector2s for each of these triggers would be added and removed from an overall value as sensed by the player.

Thanks very much,
Nick
« Last Edit: December 09, 2013, 03:07:58 PM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Vector 2 from Z rotation and magnitude
« Reply #1 on: December 04, 2013, 06:51:38 AM »
Hi,

 I would not use vector2 here:

 a rotation in unity 2 is only a float, not a vector, it represent the z axis rotation of your sprite.

then you simply store the rotation and the magnitude as two separated float. I don't see the need to store both values into a vector2.

 If that's not suitable, can you explain more about your contexct and the use case for this vector2?

bye,

 Jean

nick_h82

  • Playmaker Newbie
  • *
  • Posts: 36
Re: Vector 2 from Z rotation and magnitude
« Reply #2 on: December 04, 2013, 01:07:41 PM »
Hi Jean,

Thanks for the reply. I'd like to move an object around based on the rotation and magnitude without actually rotating it, ie apply force in a direction that corresponds to the z rotation received. Think vectored thrust on a fixed angle craft. I had imagined that storing the values as a vector2, and feeding that variable into the vector slot in an add force 2d action would fulfill this.

Maybe I'm overthinking this- would be interested if there's a simpler solution using two floats as recommended, as it's much easier for me if I can modify those floats individually.

Regards,
Nick

nick_h82

  • Playmaker Newbie
  • *
  • Posts: 36
Re: Vector 2 from Z rotation and magnitude
« Reply #3 on: December 05, 2013, 02:30:19 AM »
Hi Jean,

I've tried out your suggestion with the two floats. It appears to both set rotation correctly (set rotation action), and apply the correct amount of force (apply force 2d). I've got both actions within the same state, both update per frame, and both act on the same rigid body which is set to fixed angle. I've got debugging helpers nested inside which show the rotation is occurring.

My problem is that even though the rigid body seems to rotate properly, the force always apples in the world X direction. There's no option to apply the force to 'self', are there any other ways of doing this?

Thanks,
Nick

nick_h82

  • Playmaker Newbie
  • *
  • Posts: 36
Re: Vector 2 from Z rotation and magnitude
« Reply #4 on: December 07, 2013, 01:23:58 AM »
Couldn't get the force applied in the local space, so went back to highschool maths and got a solution working with the trig action set.

Took the local z angle, and magnitude I wanted. Then for the force to be applied in the x direction, got the cosine for the angle and multiplied by the magnitude, likewise with sine for the y. Applied both forces to the appropriate axis' in the rigid body, and it works a treat. The cool thing is that by the time the values are used, they're all just floats, I only really need to calculate the correct values once for each volume. Also avoids rotation which was my aim at the beginning.