Hello Guest

Author Topic: Angles and vectors...urgh  (Read 3487 times)

0 Members and 1 Guest are viewing this topic.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Angles and vectors...urgh
« on: July 10, 2015, 10:11:20 PM »
Its been a looong time since Ive have to use trigonometry algorithms to calculate entity movement across a scene graph, and that was in 3d.  This being a new engine to me and all, its a little different to work it out properly and to top it off, Ive forgot how to calculate and apply it anyway lol.

So im asking for help.  Does anyone have a snippet they can share of an image moving along a 2 point vector? ie: at an angle.

Im feeling a bit dense for asking because its something ive KNOWN how to do and ive forgotten. Drives you crazy.  I blame it on age.

floatvoid

  • Member
  • Posts: 11
    • View Profile
Re: Angles and vectors...urgh
« Reply #1 on: July 10, 2015, 10:29:18 PM »
You're looking for sin() and cos().

Stick this in the Update function of an entity.

Code: [Select]
            //Math functions operate in radians, but it's sometimes easier to understand degrees.
            //Multiply any degrees by this handy constant. Also RAD_TO_DEG for the opposite
            float angle = 90f * Util.DEG_TO_RAD;

            float dirX = (float)Math.Sin(angle);
            float dirY = (float)Math.Cos(angle);

            float speed = 5f;

            //now that you have a direction, you need to add it to the position each frame.
            AddPosition(dirX * speed, dirY * speed);
            // You can also do this by SetPosition( X + dirX * speed, Y + dirY * speed ).
            //Fortunately Otter has a lot of little shortcuts like this to make things easier.
           



@floatvoid on twitter

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Angles and vectors...urgh
« Reply #2 on: July 10, 2015, 11:18:33 PM »
Thats awesome thanks heaps!

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Angles and vectors...urgh
« Reply #3 on: July 11, 2015, 05:42:51 PM »
That example is perfect floatvoid. 
Another question, more Otter related.  I know you can rotate graphics using the Angle member, but can you also rotate Hitboxes?

Kyle

  • Administrator
  • Member
  • Posts: 258
    • View Profile
Re: Angles and vectors...urgh
« Reply #4 on: July 12, 2015, 07:57:32 PM »
The only colliders that can be rotated are the PolygonColliders, it has a Rotation value that can be set.  They're a little bit more expensive computationally so don't use them when it's not totally necessary.