[scripting] calculate direction between two points given the

Post » Thu Jun 21, 2012 8:06 pm

Hello everybody.
I have a math problem I need for my script. What i'm trying to do is to apply a force to an object/person that depends on the distance between me and the target and our position in the 3d space as well.
My code so far:

Event OnEffectStart(Actor Target, Actor Caster);[..... not important stuff cutted]; calculate the distance between me and the target  distance = Target.GetDistance(Caster) as int   ;Debug.Notification("Player is " + distance + " units away from the target"); get my position  myX = Caster.GetPositionX() as int  myY = Caster.GetPositionY() as int  myZ = Caster.GetPositionZ() as int; get target position  targetX = Target.GetPositionX() as int  targetY = Target.GetPositionY() as int  targetZ = Target.GetPositionZ() as int; push target to my position with a force calculated on the distance between us  ; PushActorAway must be done in order to use ApplyHavokImpulse next  Caster.PushActorAway(Target, 0)  Target.ApplyHavokImpulse(?, ?, ?, (distance/20))EndEvent

I need to calculate the ? to use in ApplyHavokImpulse, I made some tests but I can't figure out a universal formula that can suit me. I know it should be a piece of cake but now I regret every math lesson I skipped when I was at school a long time ago.
that "distance/20" is a work in progress I'll fix easily with trial end error, the direction is the issue...
tried with simply myX - targetX, myY - targetY, myZ - targetZ thinking that I was a genius but didn't work :(

(at first I tried to find the Telekinesis script to copy and modify it but I couldn't find it in the CK :biggrin:)
User avatar
Antonio Gigliotta
 
Posts: 3439
Joined: Fri Jul 06, 2007 1:39 pm

Post » Thu Jun 21, 2012 5:09 pm

hummm, lol?
tried again with:
    Target.ApplyHavokImpulse(myX-targetX, myY-targetY, myZ-targetZ, (distance))

and it seems to work, still have to be polished but the direction the target gets seems almost correct.
User avatar
CSar L
 
Posts: 3404
Joined: Fri Nov 09, 2007 9:36 pm

Post » Thu Jun 21, 2012 10:50 pm

Target.ApplyHavokImpulse((myX - targetX) / distance, (myY - targetY) / distance, (myZ - targetZ) / distance, distance * SomeForceMultiplier)
User avatar
Gemma Archer
 
Posts: 3492
Joined: Sun Jul 16, 2006 12:02 am


Return to V - Skyrim