I'm making a creeper turret that fires creeper blobs at player structures. Everything is perfect except the turret rotating correctly. The turret rotates almost opposite to where it is actually supposed to point.
Here's code:
# Creeper Sprayer.crpl
# Created on: 6/11/2017 4:39:30 PM
# ------------------------------------------
once
Self "main" "Custom0" SetImage
Self "Turret" "Custom1" SetImage
Self "main" 1 SetImagePositionZ
0 SetTimer0
endonce
GetTimer0 eq0 if
CurrentCoords 25 GetUnitCountInRange 1 gte if
CurrentCoords 25 GetUnitsInRange ->Number ->Unit
<-Unit CONST_COORDX GetUnitAttribute ->X
<-Unit CONST_COORDY GetUnitAttribute ->Y
CurrentY <-Y sub ->YY
CurrentX <-X sub ->XX
<-YY <-XX atan2 ->Angle
Self "Turret" <-Angle SetImageRotation
"CRPLCORE" CurrentCoords CreateUnit ->Blob
<-Blob "Bullet.crpl" AddScriptToUnit
<-Blob "Bullet.crpl" "targetX" <-X SetScriptVar
<-Blob "Bullet.crpl" "targetY" <-Y SetScriptVar
60 SetTimer0
endif
endif
<-Angle PI add ->Angle
...although you can inline it here.
I put that in my code and it has almost no effect.
# Creeper Sprayer.crpl
# Created on: 6/11/2017 4:39:30 PM
# ------------------------------------------
once
Self "main" "Custom0" SetImage
Self "Turret" "Custom1" SetImage
Self "main" 1 SetImagePositionZ
0 SetTimer0
endonce
GetTimer0 eq0 if
CurrentCoords 25 GetUnitCountInRange 1 gte if
CurrentCoords 25 GetUnitsInRange ->Number ->Unit
<-Unit CONST_COORDX GetUnitAttribute ->X
<-Unit CONST_COORDY GetUnitAttribute ->Y
CurrentY <-Y sub ->YY
CurrentX <-X sub ->XX
<-YY <-XX atan2 PI add ->Angle #Changed....
Self "Turret" <-Angle SetImageRotation
"CRPLCORE" CurrentCoords CreateUnit ->Blob
<-Blob "Bullet.crpl" AddScriptToUnit
<-Blob "Bullet.crpl" "targetX" <-X SetScriptVar
<-Blob "Bullet.crpl" "targetY" <-Y SetScriptVar
60 SetTimer0
endif
endif
Does this work? All it should be doing is turning the image around.
Nope, still does not work... This is what i see
Spoiler
(http://i.imgur.com/2dDR26l.png)
I'd expect that it would have been facing SW, and is now pointing NE. That being the case, substitute "PI 2 div sub" for "PI add" and it should help.
Unless I've misread the code severely, then the issue is that the Sprayer image selected as Custom1 is not facing right. So offsetting by "PI" or "PI 2 div" twists it to the expected angle.
PI 2 div sub does same as before
Pi 2 div makes the thing look up
I'm going to feel extremely stupid when I work out what I have gotten wrong.
May I have a save to fiddle with?
sure.
This one works.
# Creeper Sprayer.crpl
# Created on: 6/11/2017 4:39:30 PM
# ------------------------------------------
once
Self "main" "Custom0" SetImage
Self "Turret" "Custom1" SetImage
Self "main" 1 SetImagePositionZ
0 SetTimer0
endonce
GetTimer0 eq0 if
CurrentCoords 25 GetUnitCountInRange 1 gte if
CurrentCoords 25 GetUnitsInRange ->Number ->Unit
<-Unit CONST_COORDX GetUnitAttribute ->X
<-Unit CONST_COORDY GetUnitAttribute ->Y
CurrentY <-Y sub ->YY
CurrentX <-X sub ->XX
<-YY <-XX neg atan2 ->Angle #Changed....
Self "Turret" <-Angle SetImageRotation
"CRPLCORE" CurrentCoords CreateUnit ->Blob
<-Blob "Bullet.crpl" AddScriptToUnit
<-Blob "Bullet.crpl" "targetX" <-X SetScriptVar
<-Blob "Bullet.crpl" "targetY" <-Y SetScriptVar
60 SetTimer0
endif
endif
The issue was that CW3 (like some HTML environments) increases Y going "South" - conventionally, Y increases going "North" (PF does this). So flipping the correct sign makes the problem go away. In point of fact, this is the optimised way to flip both signs ((XX,YY) is pointing in the opposite direction) and then flip the Y sign again. Y flips twice, getting back where it started, X flips once, which is the point of the "neg" I added.
Yes... I am feeling foolish, but at least it was something I know I forgot about from not having used the CW3 editor for a while. Sorry to have taken so much of your time with trying to fix what wasn't broken.
It is ok, i had to double check some of my commands in the script because i'm used to Prpl. Thanks for the fix! :)