Self-esteem collapse when friend has "surpassed" me

I am currently going through a strange ordeal for the second time in my life, and I am wondering whether anybody else here has ever experienced anything like it. I suspect that it could be an LGB-specific issue (you will see why below), although I would be interested to learn if this is not the case.

I have an NT friend with autistic traits, the traits he has are ones I also have. I have a moderate attraction to him (which has never bothered him), though I am not sure how relevant that is to the problem, as I mentioned above. I have known him for nearly a decade, and have always considered him to be in a worse position in life than me, until recently. When some recent changes in his life meant I no longer felt that he was worse off than me, it was like something flipped in my head, and almost overnight I started obsessing over every detail of his life, wishing that it were mine. I have become fixated on obtaining as much information about everything he does as I can (as much as I can without him realising what I am doing), and that information then becomes intrusive thoughts about his successes and my failures. My self-esteem, which I thought was in the "could be worse" category before, has completely collapsed. I suddenly feel that the only way I could be valid would be to become what he is. I am also now experiencing anxiety over the possibility of the friend abandoning me, which has never been a concern for me before with this friend.

This is the second time this has happened. It previously happened with someone else (to whom I also had a moderate attraction) about 15 years ago when I was a student, and the difference then was that I immediately considered the friend who was involved on that occasion to be in a better position in life than me as soon as I met him, therefore the obsession with his life started immediately without me knowing him for years first. I remember feeling strongly that this friend was a better version of me, a vision of what I could have been if I were less autistic (and not gay).

Has anyone known anything like this before? I am assuming that it is a symptom of extremely low self-esteem. I am not sure whether it can only happen with friends who are the same gender as me (who I can therefore compare myself with more directly than a person of a different gender), and I am not sure whether it can only happen with friends who I am attracted to. I wish that I knew how to make it stop.