Knuckle Cracker

Creeper World 3 => The Coder's Corner => Topic started by: Nicant on June 11, 2017, 07:11:14 PM

Title: Opposite image rotation
Post by: Nicant on June 11, 2017, 07:11:14 PM
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
Title: Re: Opposite image rotation
Post by: GoodMorning on June 11, 2017, 07:21:34 PM
Code (in general) Select
<-Angle PI add ->Angle

...although you can inline it here.
Title: Re: Opposite image rotation
Post by: Nicant on June 11, 2017, 07:37:28 PM
I put that in my code and it has almost no effect.
Title: Re: Opposite image rotation
Post by: GoodMorning on June 11, 2017, 08:12:35 PM
# 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.
Title: Re: Opposite image rotation
Post by: Nicant on June 11, 2017, 10:09:23 PM
Nope, still does not work... This is what i see
Spoiler
(http://i.imgur.com/2dDR26l.png)
[close]
Title: Re: Opposite image rotation
Post by: GoodMorning on June 11, 2017, 10:16:06 PM
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.
Title: Re: Opposite image rotation
Post by: Nicant on June 11, 2017, 10:20:19 PM
PI 2 div sub does same as before
Pi 2 div makes the thing look up
Title: Re: Opposite image rotation
Post by: GoodMorning on June 11, 2017, 10:40:51 PM
I'm going to feel extremely stupid when I work out what I have gotten wrong.

May I have a save to fiddle with?
Title: Re: Opposite image rotation
Post by: Nicant on June 11, 2017, 10:46:25 PM
sure.
Title: Re: Opposite image rotation
Post by: GoodMorning on June 11, 2017, 11:09:14 PM
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.
Title: Re: Opposite image rotation
Post by: Nicant on June 11, 2017, 11:23:07 PM
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!  :)