Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → ApplyForce if CurDist<X From a Bullet
ApplyForce if CurDist<X From a Bullet
2004-11-03, 7:13 AM #1
So,

If a bullet is a couple feet from a player, I want the player to get shoved out of the way, I figure ApplyForce?

I define the bullet with a bullet = firstthinginview(0x8 bullet)
And then something that detects if the current distance from the bullet is less than a certain amount(About to hit the player) and shoves the player out of the way using ApplyForce....

I'm hoping to implement this in Force_Heal somehow, make it togglable, and have it consume Force Mana. (If this is too hard than dont worry about it)

Any help would be great!

P.S. It would be nice to Have it apply force in the direction where there are no walls, so I dont get slammed against a wall.

[T0rN]
2004-11-03, 11:02 AM #2
Code:
# 
#
# FORCE_HEALING.COG
#
# FORCEPOWER Script -  Bullet Dodger
#  Light Side Power
#  Bin 25
#   Toggleable force power that will make the player dodge 
#   bullets. Currently uses a lame crossvector method to
#   create the dodge vector. 
#
#   needs a check to see if the player is gonna smash into
#   a wall, everything else should be there
# [Compos Mentis/Maggot]
#
# (C) 1997 LucasArts Entertainment Co. All Rights Reserved


symbols

thing       player                           local

flex        cost=000.0                       local
int         rank                             local
flex        mana                             local
flex        frequency=0.1                    local

sound       DodgeSound=yoursound.wav         local
int         soundChannel=-1                  local

template    sparks=+ssparks                  local

vector      position                         local

message     startup
message     activated
message     selected
message     newplayer
message     pulse
message     timer
message     killed

end

# ========================================================================================

code

startup:
   player = GetLocalPlayerThing();
   call stop_power;

   Return;

# ........................................................................................

activated:
   mana = GetInv(player, 14);
   if((!IsInvActivated(player, 25)) && (GetThingHealth(player) > 0) && (mana >= cost))
   {
       SetPulse(frequency)
   }
   else
   {
       call stop_power;
   }

   Return;

selected:
   Print("Bullet Dodger");
   Return;

newplayer:
   call stop_power;
   Return;

pulse:
   DodgeThis = FirstThingInView(Player, 179, 2, 0x8); // check for bullets nearby
   if((DodgeThis != -1) && (GetThingParent(DodgeThis) != Player)) //if we have a bullet and it isn't from the player
   {
      BulletPos = GetThingPos(DodgeThis);
      CurPos = GetThingPos(Player);
      CurCollide = GetCollideType(DodgeThis); // check the bullets old collide type
      BulletDist = VectorSub(CurPos, BulletPos); // Distance from bullet
      if(BulletDist <= 1) // if the bullet is within 1 jku
        {                                                             
            DodgeVec = VectorNorm(VectorCross(CurPos, BulletPos)); // use the crossvector as your dodge vector
   	    NewVec = VectorScale(DodgeVec, 120);                   // these lines could be changed to a different method
            ApplyForce(player, NewVec);
 	    SetCollideType(DodgeThis, 0); // temporarily make the bullet uncollidable
	    SetTimerEx(.75, DodgeThis, BulletDist, CurCollide); // set the bullets timer and pass on the params
        }
   }
   DodgeThis = -1; // reset target
   Return;

timer:
   SentMe = GetSenderId();
   OldBulletDist = GetParam(0);
   OldCollide = GetParam(1);
   NewBulletDist = VectorSub(GetThingPos(player), GetThingPos(SentMe));
   if(NewBulletDist > OldBulletDist) // if the bullet is farther away than it was last time around
      {
         KillTimerEx(SentMe); // kill it's timer
	 SetCollideType(SentMe, OldCollide); // and reset it's collide
      }
   Else
      {
         SetTimerEx(.5, SentMe, NewBulletDist, OldCollide); // reset the timer for another pass
      }
   Return;

killed:
   if(GetSenderRef() != player)
   Return;

stop_power:
   SetPulse(0);
   SetInvActivated(player, 25,0);
   Return;

end


I'm rusty with cog, but this should work.
Let me know how it works out
</sarcasm>
<Anovis> mmmm I wanna lick your wet, Mentis.
__________
2004-11-03, 3:28 PM #3
Nothing. Dosent even say "Bullet Dodger". I really appreaciate the help though, And I"m going to try and screw around with this though, and any help would still be great.

[T0rN]
2004-11-03, 4:48 PM #4
Code:
#
#
# FORCE_HEALING.COG
#
# FORCEPOWER Script -  Bullet Dodger
#  Light Side Power
#  Bin 25
#   Toggleable force power that will make the player dodge
#   bullets. Currently uses a lame crossvector method to
#   create the dodge vector.
#
#   needs a check to see if the player is gonna smash into
#   a wall, everything else should be there
# [Compos Mentis/Maggot]
#
# (C) 1997 LucasArts Entertainment Co. All Rights Reserved
#==============================================================#
symbols

thing     player            local

flex      cost=000.0        local
flex      mana              local
flex      frequency=0.1     local
flex      dodgethis         local
flex      curcollide        local
flex      bulletdist        local
flex      sentme            local
flex      oldbulletdist     local
flex      oldcollide        local
flex      newbulletdist     local

vector    CurPos            local
vector    bulletpos         local
vector    dodgevec          local
vector    newvec            local

template  sparks=+telesparks   local

message   startup
message   activated
message   selected
message   newplayer
message   pulse
message   timer
message   killed

end
#==============================================================#
code
#------------------------------------------------------
startup:
	player = GetLocalPlayerThing();
	call stop_power;

Return;
#------------------------------------------------------
activated:
	mana = GetInv(player, 14);
	if((!IsInvActivated(player, 25)) && (GetThingHealth(player) > 0) && (mana >= cost))
	{
		SetPulse(frequency);
		SetInvActivated(player, 25, 1);
	}
	else
	{
		call stop_power;
	}

Return;
#------------------------------------------------------
selected:
	Print("Bullet Dodger");

Return;
#------------------------------------------------------
newplayer:
	call stop_power;

Return;
#------------------------------------------------------
pulse:
	dodgethis = FirstThingInView(player, 179, 2, 0x8);	// check for bullets nearby
	if((dodgethis != -1) && (GetThingParent(dodgethis) != player))	//if we have a bullet and it isn't from the player
	{
		bulletpos = GetThingPos(dodgethis);
		CurPos = GetThingPos(player);
		curcollide = GetCollideType(dodgethis);	// check the bullets old collide type
		bulletdist = VectorLen(VectorSub(CurPos, bulletpos));	// Distance from bullet
		if(bulletdist <= 1)	// if the bullet is within 1 jku
		{
			dodgevec = VectorNorm(VectorAdd(CurPos, bulletpos));	// use the dotvector as your dodge vector
			newvec = VectorScale(dodgevec, 120);	// these lines could be changed to a different method
			CreateThing(sparks, player);
			ApplyForce(player, newvec);
			SetCollideType(dodgethis, 0);	// temporarily make the bullet uncollidable
			SetTimerEx(.75, dodgethis, bulletdist, curcollide);	// set the bullets timer and pass on the params
		}
	}
	dodgethis = -1;	// reset target

Return;
#------------------------------------------------------
timer:
	sentme = GetSenderID();
	oldbulletdist = GetParam(0);
	oldcollide = GetParam(1);
	newbulletdist = VectorLen(VectorSub(GetThingPos(player), GetThingPos(sentme)));
	if(newbulletdist > oldbulletdist)	// if the bullet is farther away than it was last time around
	{
		KillTimerEx(sentme);	// kill it's timer
		SetCollideType(sentme, oldcollide);	// and reset it's collide
	}
	else
	{
		SetTimerEx(.5, sentme, newbulletdist, oldcollide);	// reset the timer for another pass
	}

Return;
#------------------------------------------------------
killed:
	if(GetSenderRef() != player) Return;
	stop_power:
	SetPulse(0);
	SetInvActivated(player, 25, 0);

Return;
#------------------------------------------------------
end


Tested and working. But be careful, as it doesn't check for cliffs/walls. The force applied isn't strong enough to hurt you, but a fall off a cliff will.
</sarcasm>
<Anovis> mmmm I wanna lick your wet, Mentis.
__________
2004-11-03, 6:25 PM #5
Thank you very much. This is great, I've wanted this effect for a long time.

Btw, is MSN working for you again?

[T0rN]
2004-11-03, 6:36 PM #6
Ahh shoot. Still nothing, sorry man. What I did was copy your code into a blank force_heal.cog, made sure there were no wrapping problems, and loaded up JK. Same as before. Heal is just a void, dosent say anything at all. Any ideas? Lol.
2004-11-03, 6:37 PM #7
eah_maggot@hotmail.com
What's your passport, I may have inadvertantly blocked you when I was going through removing/blocking people I didn't think I knew.
</sarcasm>
<Anovis> mmmm I wanna lick your wet, Mentis.
__________
2004-11-03, 6:40 PM #8
Lol you must be on within minutes of me... Anymore ideas on the cog?
2004-11-03, 6:56 PM #9
I've been trying to figure out a method to see if there's a cliff in the direction of the dodge vector. Also, I'm trying to figure out a way to check for walls.
</sarcasm>
<Anovis> mmmm I wanna lick your wet, Mentis.
__________
2004-11-04, 6:14 AM #10
Maggot you did it. Beautiful cog.
This cog is a work of art.

Thank you very much.

If you release another version, that would be cool too, but this is awsome, with exactly the effect wanted.

Thanks again.

[T0rN]
2004-11-04, 8:42 PM #11
To check for walls, fire an invisible projectile in the "force dodge" direction. Give it a reasonable speed (so it will be quick enough) and a short timer (so it will not hang around if it does not hit a wall nearby). In the projectile's "removed" handler, check the distance from itself to player. If within distance, don't force move that way.

To check for cliffs, fire an invisible projectile down at the expected "force dodge" distance away from player, from about waist height. Do similar tasks as the wall check.

Also, there were a few threads about dodging bullets within the last few months; maybe they will have helpful info.

:)
2004-11-04, 9:50 PM #12
Thanks for the suggestions. Sounds like they'll work perfectly, I'll try putting em in when I get around to it :)
</sarcasm>
<Anovis> mmmm I wanna lick your wet, Mentis.
__________

↑ Up to the top!