playMaker

Author Topic: Algorythm to align objects without using compare ???  (Read 5306 times)

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Algorythm to align objects without using compare ???
« on: May 03, 2013, 08:35:35 AM »
Fighting from yesterday on this, wasted surely more than 10h but apparently my brain can't catch up with it.

I want to do this: I have camera with its orientation, and I have tank turret with its orientation and I want the turret to slowly rotate to the same direction of the camera.

I put the Y world rotation of both objects into 2 variables.

Now, if I could use IF/THEN, this wouldn't be a problem, because I would just compare the 2 values and add torque to the turret as desired.

But having to deal with angles, clamping, etc. totally wasted me.

It's at least 10 hours I'm losing over this stupid function, and all I got is something "half working", with the turret that rotates correctly only on 180 degrees, and doesn't stop on the other 180° arc.

Does anyone know the exactl algorythim to deal with this thing?

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #1 on: May 03, 2013, 12:11:02 PM »
OK, catched up with a solution that almost works... when the vehicle is horizontal.

When the vehicle rotates on different axis from Y, it's messed up again.

Now a question: does the get rotation in WORLD mode gets informations on the projection of the angles respect to WORLD or not?

Because when the tank rotates on the X or Z axis, the change in Y shouldn't ever be so massive.

You can check the behaviour here: https://dl.dropboxusercontent.com/u/858716/unityhelp/PlayMaker/TP32.rar

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Algorythm to align objects without using compare ???
« Reply #2 on: May 09, 2013, 06:13:46 AM »
hi,

what happens if you just extract the y euler rotation of the camera and apply it to the tank turret?

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #3 on: May 09, 2013, 09:19:44 AM »
It can't be like that, it can't be instant.
I need to add torque to the turret to reach slowly the camera alignment.

But thank you to point me out to the euler stuff, I was using XYZ angles up to now.
I'll check if I can come out with this later.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Algorythm to align objects without using compare ???
« Reply #4 on: May 10, 2013, 04:14:45 AM »
hi,

 that's fine, you can still get the y angle, and smooth it out before you inject it back to the turret.

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #5 on: May 10, 2013, 08:50:11 AM »
You mean by extracting from euler angles?
How do I do that?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Algorythm to align objects without using compare ???
« Reply #6 on: May 11, 2013, 08:46:33 AM »
Hi,

 Use the action "Get Rotation" and get the "Y rotation", this is your euler angle. "Euler" is subject to problems sometimes ( the gimbal lock effect), but in your case, I think it will just do fine.

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #7 on: May 13, 2013, 12:15:07 PM »
I was already reading those angles but I thought they were from the vector3 variable.

In fact I got this working almost immediately.

I don't know if we are talking about the "gimbal lock" effect here, but the problem comes when the objects are near the 360 limit.

In example, if the camera is aligned at 5 degrees, and the turret is positioned at 355 degrees, that's only 10 degrees rotation, but the turret will start rotate to the opposite direction and I really don't know how to solve this without IF/THEN.
« Last Edit: May 13, 2013, 12:45:52 PM by megmaltese »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Algorythm to align objects without using compare ???
« Reply #8 on: May 14, 2013, 03:26:05 AM »
Hi,

 I think you are facing the gimbal lock ( definitly not sure, cause I have a hard time figuring out what exactly is the gimbal lock, but that's very likely the case here).

 You'll have to use quaternion to avoid this kind of issues, OR have a gameObject acting as a target attached to the camera and far in front, then the turret simply look at this target, simple and effective.

With quaternion, I have created a set of custom action to do everything you need pretty much

https://hutonggames.fogbugz.com/default.asp?W967


also as I think of this out loud, I think you could get away with "smooth look at Direction", I think this is what you need infact.

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #9 on: May 14, 2013, 04:00:09 AM »
Mhhh I don't think it's the gimbal lock, because the turret only rotates around one axis, and the camera as well, and the tank is mostly horizontal.

The problem is simply the fact that I don't know how to play with angles in this case.
I'm sure there's some simple solution to this but unluckily I have no technical school about this and I'm lost.

I'll see if I can understand how quaternion work by myself and let you know if I manage to pull this thing off with those juicy commands.



On the other side, I would like to make ALL physical, including the turret rotation.
This means I want to add TORQUE to the turret and this for a simple reason: the cannon is long, and can impact on structures.
If the turret is not managed physically, the cannon will be able to push the tank away easily and this can't happen.
Yes I could make that if the cannon impacts something it stops moving, but that's a dirty trick and I don't like it much.
If I go physical, I'll have no problems at all with this.

And once I learn how to deal with this stuff, it's forever :D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Algorythm to align objects without using compare ???
« Reply #10 on: May 14, 2013, 04:07:14 AM »
Hi,

 then you can try to use this:

http://wiki.unity3d.com/index.php/TorqueLookRotation

bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #11 on: May 14, 2013, 04:39:21 AM »
Eh, thank you for the effort, but this works on all 3 axis, I need to rotate only on one axis. Tried to apply this to my tank, it flies away in funny ways   :D

And on top of this, I must do my own action or FSM because I have my tank's meshes axis all messed up  ;D

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Algorythm to align objects without using compare ???
« Reply #12 on: May 14, 2013, 04:49:47 AM »
I'm studying quaternions here: http://physicsforgames.blogspot.it/2010/02/quaternions.html

Should be enough to begin  :D