Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Nicant on December 23, 2015, 04:59:26 PM

Title: Any way to have an image rotate and face a unit?
Post by: Nicant on December 23, 2015, 04:59:26 PM
I am trying to make a creeper gun unit that fires on player made units, i have gotten the firing part done, now all i need is to have a way for the barrel to rotate and face the player made units. Any help will be greatly appreciated and be credited.  :)
Title: Re: Any way to have an image rotate and face a unit?
Post by: Grayzzur on December 23, 2015, 07:42:34 PM
Virgil's Alpha Sector map Chimera has gun/barrel code that does that. It's been copied into many maps.
Title: Re: Any way to have an image rotate and face a unit?
Post by: GameGibu on January 05, 2016, 02:07:50 AM
Trigonometry. Math. Calculus be good too, but ye not necessarily be needin' it fer this here job.

And not to forget, thar be SetImageRotation.

#Self is Self-explanatory
#"main" actually could be anything as long as it matches what you used in SetImage if you used that. If you just used the ingame selection, this does have to be "main", though your whole image will rotate.
#rotationInRadians this is what you calculate with trig, namely atan2.
#I'm assuming that you have successfully detected your target unit.
->TargetUnit
<-TargetUnit CONST_COORDY GetUnitAttribute CurrentX sub ->xDIST
<-TargetUnit CONST_COORDY GetUnitAttribute CurrentY sub ->yDIST
atan2(y2 CurrentY sub x2 CurrentX sub) ->result
SetImageRotation(Self "main" <-result)


Aye, and a good day to ye!
-GameGibu

Yarr...
Title: Re: Any way to have an image rotate and face a unit?
Post by: Nicant on January 05, 2016, 09:35:56 PM
Quote from: GameGibu on January 05, 2016, 02:07:50 AM
Trigonometry. Math. Calculus be good too, but ye not necessarily be needin' it fer this here job.

And not to forget, thar be SetImageRotation.

#Self is Self-explanatory
#"main" actually could be anything as long as it matches what you used in SetImage if you used that. If you just used the ingame selection, this does have to be "main", though your whole image will rotate.
#rotationInRadians this is what you calculate with trig, namely atan2.
#I'm assuming that you have successfully detected your target unit.
->TargetUnit
<-TargetUnit CONST_COORDY GetUnitAttribute CurrentX sub ->xDIST
<-TargetUnit CONST_COORDY GetUnitAttribute CurrentY sub ->yDIST
atan2(y2 CurrentY sub x2 CurrentX sub) ->result
SetImageRotation(Self "main" <-result)


Aye, and a good day to ye!
-GameGibu

Yarr...

Thank you for making this GameGibu, You will be credited in the map i am making! (Not the creeper islands map, but the map after creeper islands which is coming soon! :) )
Title: Re: Any way to have an image rotate and face a unit?
Post by: GameGibu on January 05, 2016, 10:40:49 PM
Just a note, I wrote that code from scratch, entirely in the forum post box, on my mobile.  :o
It exists only here unless you have copied it already.
But maybe I should copy it because it looks so shiny and refined ;)

Yay me! Yay you! Yay everyone!  ;D ;D ;D
-GameGibu