Adam's Lair Forum

game development and casual madness
It is currently 2020/01/28, 18:18

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: 2014/07/27, 03:13 
Novice Member
Novice Member

Joined: 2014/07/27, 02:41
Posts: 11
Role: Hobbyist
Hey All,
I'm developing a not-so-simple topdown game in Duality (a clone of the amazing Z-Type, developed in js [link below]) and struggled for a while to make my ship gameObject "face" another gameObject, this is the snippet that I eventually found to do the trick.
Code:
OpenTK.Vector2 face = [yourgameobj].Transform.Pos.Xy -[yourtarget].Transform.Pos.Xy;
[yourgameobj].Transform.Angle = (float)Math.Atan2(face.Y,face.X)-(float)(Math.PI/2);


UPDATE: (after a bit of a dicussion, this is the updated snippet, given unto us by adam himself, and seems to work given that your sprite image faces "up", otherwise you'd still need to add a flat value.

Code:

//Notice that this vector is Target Relative to Your Object
OpenTK.Vector2 face = [targetObj].Transform.Pos.Xy - [yourObj].Transform.Pos.Xy;           

//Vector2 already wraps all of the angle magic. duh.
[yourObj].Transform.Angle = face.Angle;


Anyone with basic vector knowledge would be able to come to the vector subtraction conclusion (like I did!), the rotation still wouldn't work right no matter how much I tried. After positioning the target directly besides my "ship" I realized that the difference was exactly PI/2 rad in rotation, and hacked that into the code.
UPDATE: check viewtopic.php?f=17&t=257&p=943#p942 to see why this happens.

Now, I would love to summon whatever gods reside here to discuss whether I did something wrong or failed to grasp some of the engine's concepts, but this is what works for now as seen in [link below]. I would be glad to edit this OP if we eventually get to a better and less hackish solution, gotta help searchers and googlers, right? :mrgreen:

Cheers,
PR


Last edited by pedromundo on 2014/07/27, 16:27, edited 3 times in total.

Top
 Profile  
 
PostPosted: 2014/07/27, 03:13 
Novice Member
Novice Member

Joined: 2014/07/27, 02:41
Posts: 11
Role: Hobbyist
Links:
Original Z-Type: http://phoboslab.org/ztype/
Woking Screenshot: http://snag.gy/55VRI.jpg


Top
 Profile  
 
PostPosted: 2014/07/27, 10:49 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
I'd say that the reason is that the sprite's "front" should be along the X+ axis while your sprite is drawn so that the front of the ship is on top and the rear on bottom (i.e. turned exactly Pi/2 respect to the sprite's "front"), which is fine if you don't need to take into account the relative direction of the ship.

Or, seeing it the other way, angle 0 stands on the X axis, so your sprite should face that way

See this picture for reference: on the left is the "traditional" way of representing angles. In computer graphics (and, so, in Duality as well), it is like the right side. Front is on the right, and angles increasing going clockwise.

Image

Your -Pi/2 is basically compensating for the wrong orientation of your sprite

Hope I made myself clear :mrgreen:

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2014/07/27, 14:02 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
SirePi already explained a lot, but I'll just add some general information on how stuff like that works in Duality - which may be confusing, because some details differ from the "math standard" you might be expecting.

  • Coordinate Axes: In math, Y usually points upwards and X to the right. In Duality, being a 2D game engine, Y points downwards. If an object moves to the bottom right in world space, it continuously increases both X and Y coordinates.
  • Angles: In math, zero degree angles point to the right and increase counterclockwise. In Duality, zero degree point to the top and increase clockwise.

So, when creating sprites, their top / front should always point to the top of the sprite image in order to align with Duality's axes and angles. As a bonus, you can then use a lot of math functionality that Duality has already prepared for you. Given the above requirements, this should let object A face object B:

Code:
Vector2 bRelativeToA= objB.Transform.Pos.Xy - objA.Transform.Pos.Xy;
objA.Transform.Angle = bRelativeToA.Angle;


Also, Duality is completely powered by single precision floating points calculations, i.e. float. Since .Nets Math class returns double in a lot of cases, Duality provides the MathF helper class you can use. It mirrors Math functionality as float versions and also provides some extra functionality.

Hope we could clear some things up for you. And welcome to the forum btw :)

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2014/07/27, 14:18 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
Adam wrote:
Angles: In math, zero degree angles point to the right and increase counterclockwise. In Duality, zero degree point to the top and increase clockwise.


Meaning what I said is wrong :redface:

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2014/07/27, 15:11 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
SirePi wrote:
Adam wrote:
Angles: In math, zero degree angles point to the right and increase counterclockwise. In Duality, zero degree point to the top and increase clockwise.


Meaning what I said is wrong :redface:


Well. At least you're wrong by exactly 90 degrees, so you've got math precision on your side. :D

To be honest, I just skimmed your posting and assumed you were talking about a specific error case where sprites are oriented the wrong way, which made total sense to me. I've read it thoroughly now and it seems you're a little off regarding Duality angles, but your traditional "math" angle description appears to be correct.

Now let's hope we didn't confuse pedromundo even more. xD

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
PostPosted: 2014/07/27, 16:18 
Novice Member
Novice Member

Joined: 2014/07/27, 02:41
Posts: 11
Role: Hobbyist
Glad to see such lively discussions on vectors, coordinate spaces and whatnot around here.

So, I had already kinda figured out, by trial and error, that Duality's angles started pointing up and increased clockwise, so there's that :D

WHAT I didn't realize though is that OpenTK's Vector2s had an angle propety (even though that property is present in all of duality's vectors lol), by using that we save ourselves a lot of the math.

Now, I'll update the op to include the newer and more elegant solution; but considering that my sprite was facing the right direction, why'd I have to add PI/2 anyways?


Top
 Profile  
 
PostPosted: 2014/07/27, 16:24 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
Hey guys, I think I figured it out, and this might be cause a lot of misunderstanding if one is not expecting it.

I tried a simple test, and it appears that Atan2 works, as I said, assuming 0 degrees on the X+ axis, while Vector2 Class assumes 0 as Y- !!! (in fact, in my code I usually used Atan2 since I didn't know about Vector2.Angle, as I rarely use Vector2s and that's why I always had my sprites oriented to the right :D)

Simple explanation:

Code:
// using Vector2.Angle
Vector2.UnitX.Angle          [1, 0]     1.57079637     (a)
Vector2.UnitY.Angle          [0, 1]     3.14159274     (b)
(-Vector2.UnitY).Angle       [0, -1]    0.0            (c)

// using MathF.Atan2(y, x)
MathF.Atan2(0, 1)            [1, 0]     0.0            (a)
MathF.Atan2(1, 0)            [0, 1]     1.57079637     (b)
MathF.Atan2(-1, 0)           [0, -1]   -1.57079637     (c)


I learned something new today as well :mrgreen:

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2014/07/27, 16:25 
Novice Member
Novice Member

Joined: 2014/07/27, 02:41
Posts: 11
Role: Hobbyist
@SirePi OH! Ain't that a lovely thing to find out? :mrgreen:
(updated the OP to include a link to that, so that googlers (such as myself before trying to fix it myself) can eventually find it)


Top
 Profile  
 
PostPosted: 2014/07/27, 16:30 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
pedromundo wrote:
Now, I'll update the op to include the newer and more elegant solution; but considering that my sprite was facing the right direction, why'd I have to add PI/2 anyways?


Do you mean the "correct" direction (i.e. facing up) or the "right" direction (i.e. facing right?) :D

in the first case, -Pi/2 is what is needed to compensate for the difference in behavior between Vector2.Angle and Atan2 (see my previous post)
EDIT:
In addition, in your first version of the post you calculated the direction as Source - Target, which is wrong. The correct way is, as you did now, Target - Source :)

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group