Jump to content

[LE] trigonometry and Angle question


Recommended Posts

I try to create a new spell that can cause a series of explosions around caster like a circle.

I have tested it many times. In some place, the circle is completed, but the circle sometimes is incomplete or explosions sometimes will all stay in a single place. I think this must be something wrong with my formula, but I just can't figure it out. Really need someone to take look my script.

Event OnEffectStart(Actor akTarget,Actor akCaster)
	Caster = akCaster
	ActivatorRef = Caster.PlaceAtMe(AA_EmptyActivator)
	Ang = ((360 - Caster.GetAngleZ())/18)
	OriginAng = Caster.GetAngleZ()
	Count = 0
	While count <= 18
		count = count+1
		FinalAng = OriginAng + Ang*count
			ActivatorRef.MoveTo(Caster,200*Sin(FinalAng),200*Cos(FinalAng),Caster.GetHeight())
			ActivatorRef.PlaceAtMe(AA_FireExplosion)
		wait(0.1)
	EndWhile
EndEvent
Link to comment
Share on other sites

I think my mind just get stuck. I made some changes and it worked.

Event OnEffectStart(Actor akTarget,Actor akCaster)
	Caster = akCaster
	ActivatorRef = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	OriginAng == Caster.GetAngleZ()
	Count = 0
	While count <= 18
		count = count+1
		if (OriginAng + count*20)<360
			ActivatorRef.MoveTo(Caster,200*Sin(OriginAng + count*20),200*Cos(OriginAng + count*20),Caster.GetHeight())
			ActivatorRef.PlaceAtMe(AA_PentagramExp)
		else
			ActivatorRef.MoveTo(Caster,200*Sin((OriginAng + count*20)- 360),200*Cos((OriginAng + count*20)- 360),Caster.GetHeight())
			ActivatorRef.PlaceAtMe(AA_PentagramExp)
		endif
		wait(0.1)
	EndWhile
EndEvent
Link to comment
Share on other sites

hmm.. Are you sure?

OriginAng == Caster.GetAngleZ()

ughPentagramSpellScript

 

Scriptname ughPentagramSpellScript extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/9130703-trigonometry-and-angle-question/

  Activator PROPERTY AA_PentagramEmptyActivator auto
  Explosion PROPERTY AA_PentagramExp            auto

  ObjectReference ActivatorRef        ; object holder


; -- EVENT --

EVENT OnEffectStart(Actor akTarget,Actor akCaster)
    ActivatorRef = akCaster.PlaceAtMe(AA_PentagramEmptyActivator as Form)        ; place special activator near the caster
    myF_Place(akCaster as ObjectReference)
ENDEVENT


; -- FUNCTION --

;--------------------------------------------
FUNCTION myF_Place(ObjectReference casterRef)
;--------------------------------------------
    float fZ = casterRef.GetAngleZ()        ; fZ = OriginAng
    float fH = casterRef.GetHeight()        ; get height of caster once (outer the loop)

int n = 20                                  ; init by 20 degree
    WHILE (n <= 360)
        float f = fZ + (n as Float)         ; f = OriginAng + count*20
        IF (f > 360.0)
            f = f - 360.0
        ENDIF

        IF ( ActivatorRef )
            ActivatorRef.MoveTo(casterRef, Math.SIN(f) * 200.0, Math.COS(f) * 200.0, fH)
            ActivatorRef.PlaceAtMe(AA_PentagramExp as Form)                     ; place explosion near the moved activator
            Utility.Wait(0.1)
        ENDIF

        n = n + 20            ; 40°, 60°, .. 360° (380°)
    ENDWHILE

; maybe next is useful to clean somethings as fast as possible
    IF ( ActivatorRef )
        ActivatorRef.DisableNoWait()
        ActivatorRef.Delete()
        ActivatorRef = None
    ENDIF
ENDFUNCTION

 


Edited by ReDragon2013
Link to comment
Share on other sites

yeah, you are right. I made some change based on your script. And now it works fine. And also I would like to know if there is a particular reason to use the function. The only thing I know about function is to reduce the size of a script. For example,

Code 1:....................................................................................

If code 1 need to be used more than once in the script, I can create a function that contain this code and use this function instead code 1. Function can significantly reduce the size of a script and this is only advantage that I know about. However, I'm sure that a function can do even more than this. If there is any resources I can look at, please let me know. I do really appreciate your help

ScriptName AA_PentagramScript extends ActiveMagicEffect
Import Math
Import Utility

Activator Property AA_PentagramEmptyActivator auto
Spell property AA_PentagramExpSpell auto

Actor Caster
ObjectReference ActivatorRef
ObjectReference ActivatorRef022
ObjectReference ActivatorRef033
ObjectReference ActivatorRef044
ObjectReference ActivatorRef055

Event OnEffectStart(Actor akTarget,Actor akCaster)
	Caster = akCaster
	ActivatorRef = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	ActivatorRef022 = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	ActivatorRef033 = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	ActivatorRef044 = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	ActivatorRef055 = Caster.PlaceAtMe(AA_PentagramEmptyActivator)
	float OriginAng = Caster.GetAngleZ()
	float CasterHeight = Caster.GetHeight()
	int Count = 0
	While count <= 18
		count = count+1
		float f = OriginAng + 20*(count as float)
		if (f)<360
			ActivatorRef.MoveTo(Caster,200*Sin(f),200*Cos(f),CasterHeight)
			AA_PentagramExpSpell.RemoteCast(ActivatorRef,Caster)
		else
			f = f - 360
			ActivatorRef.MoveTo(Caster,200*Sin(f),200*Cos(f),CasterHeight)
			AA_PentagramExpSpell.RemoteCast(ActivatorRef,Caster)
		endif
		wait(0.25)
	EndWhile
	Count = 0
	float Plus = 0
	While count <= 3
		count = count+1
		Plus = Plus+ 200
		ActivatorRef.MoveTo(Caster,Plus*Sin(OriginAng),Plus*Cos(OriginAng),CasterHeight)
		AA_PentagramExpSpell.RemoteCast(ActivatorRef,Caster)
		ActivatorRef022.MoveTo(Caster,Plus*Sin(OriginAng+ 60),Plus*Cos(OriginAng+ 60),CasterHeight)
		AA_PentagramExpSpell.RemoteCast(ActivatorRef022,Caster)
		ActivatorRef033.MoveTo(Caster,Plus*Sin(OriginAng- 60),Plus*Cos(OriginAng- 60),CasterHeight)
		AA_PentagramExpSpell.RemoteCast(ActivatorRef033,Caster)
		ActivatorRef044.MoveTo(Caster,Plus*Sin(OriginAng+ 140),Plus*Cos(OriginAng+ 140),CasterHeight)
		AA_PentagramExpSpell.RemoteCast(ActivatorRef044,Caster)
		ActivatorRef055.MoveTo(Caster,Plus*Sin(OriginAng- 140),Plus*Cos(OriginAng- 140),CasterHeight)
		AA_PentagramExpSpell.RemoteCast(ActivatorRef055,Caster)
		wait(0.5)
	EndWhile
	ActivatorRef.Delete()
	ActivatorRef022.Delete()
	ActivatorRef033.Delete()
	ActivatorRef044.Delete()
	ActivatorRef055.Delete()
EndEvent
Link to comment
Share on other sites

The benefit of functions is mostly to have small and clean content of events. If you like to put all the code into the event, if it works nobody will blame you.

My way of papyrus coding is not always intuitive. Many ways lead to Rome.

 

Maybe next code helps to understand the sense of functions. I also try to show you the benefit of arrays.

 

AA_PentagramScript

 

Scriptname AA_PentagramScript extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/9130703-trigonometry-and-angle-question/

  Activator PROPERTY AA_PentagramEmptyActivator auto
  Spell     PROPERTY AA_PentagramExpSpell       auto

  ObjectReference[] a                ; value holder for placed activator Refs
      ; a[0] = ActivatorRef                
      ; a[1] = ActivatorRef022
      ; a[2] = ActivatorRef033
      ; a[3] = ActivatorRef044
      ; a[4] = ActivatorRef055


; -- EVENT --

EVENT OnEffectStart(Actor akTarget,Actor akCaster)
    myF_Action(akCaster as ObjectReference)
ENDEVENT


; -- FUNCTIONs -- 2

;---------------------------------------------
FUNCTION myF_Action(ObjectReference casterRef)
;---------------------------------------------
    a = new ObjectReference[5]            ; init array by five (a[0] .. a[4])
    objectReference oRef

int i = 0
    WHILE (i < 5)
        oRef = casterRef.PlaceAtMe(AA_PentagramEmptyActivator)
        a[i] = oRef
        i = i + 1
    ENDWHILE
    
    float fZ = casterRef.GetAngleZ()                ; fZ = OriginAng
    float fH = casterRef.GetHeight()                ; fH = CasterHeight
    float fp = 200.0                                ; fp = Plus

; ---------------- circle of death (divided in parts of 20 degree)
    i = 0                                           ; i = count
    WHILE (i < 19)
        i = i + 1

        float f = ((i as Float) * 20.0) + fZ
        IF (f >= 360.0)
            f = f - 360.0
        ENDIF

        myF_Remote(casterRef, 0, fp, fH, f)
;;;        a[0].MoveTo(casterRef, Math.SIN(f) * fp, Math.COS(f) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[0], casterRef)

        Utility.Wait(0.25)
    ENDWHILE

; --------------- fp is already 200.0
    i = 0                                            ; i = count (0..3)
    WHILE (i < 4)
        myF_Remote(casterRef, 0, fp, fH, fZ)
        myF_Remote(casterRef, 1, fp, fH, fZ + 60.0)
        myF_Remote(casterRef, 2, fp, fH, fZ - 60.0)
        myF_Remote(casterRef, 3, fp, fH, fZ + 140.0)
        myF_Remote(casterRef, 4, fp, fH, fZ - 140.0)

;;;        a[0].MoveTo(casterRef, Math.SIN(fZ) * fp, Math.COS(fZ) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[0], casterRef)

;;;        a[1].MoveTo(casterRef, Math.SIN(fZ + 60.0) * fp, Math.COS(fZ + 60.0) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[1], casterRef)

;;;        a[2].MoveTo(casterRef, Math.SIN(fZ - 60.0) * fp, Math.COS(fZ - 60.0) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[2], casterRef)

;;;        a[3].MoveTo(casterRef, Math.SIN(fZ + 140.0) * fp, Math.COS(fZ + 140.0) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[3], casterRef)

;;;        a[4].MoveTo(casterRef, Math.SIN(fZ - 140.0) * fp, Math.COS(fZ - 140.0) * fp, fH)
;;;        AA_PentagramExpSpell.RemoteCast(a[4], casterRef)

        Utility.Wait(0.5)
        fp = fp + 200.0
        i = i + 1
    ENDWHILE

; --------------- clean up placed objects
    i = 0
    WHILE (i < 5)
        oRef = a[i]                      ; get array entry
        oRef.Delete()                    ; mark this object for delete
        oRef = None                      ; remove persistence
        a[i] = oRef                      ; clean out array entry
        i = i + 1
    ENDWHILE
ENDFUNCTION


;----------------------------------------------------------------------------------
FUNCTION myF_Remote(ObjectReference casterRef, Int i, Float fp, Float fH, Float fZ)  ; helper
;----------------------------------------------------------------------------------
; https://www.creationkit.com/index.php?title=RemoteCast_-_Spell

IF ( a )
    a[i].MoveTo(casterRef, Math.SIN(fZ) * fp, Math.COS(fZ) * fp, fH)
    AA_PentagramExpSpell.RemoteCast(a[i], casterRef as Actor)
ENDIF
ENDFUNCTION

 

 

Edited by ReDragon2013
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...