View unanswered posts | View active topics It is currently Thu Dec 26, 2024 4:55 pm



Reply to topic  [ 7 posts ] 
 How to make an object bounce off terrain? 
Author Message

Joined: Sat Jan 13, 2007 11:04 pm
Posts: 2932
Reply with quote
Post How to make an object bounce off terrain?
I'm trying to create a gun that causes the same effect that halflife 2 had when someone was hit by one of those funny energy orbs. I have the gun and effects working and all, but I dont know how to make it bounce of terrain.

So all I need is the code for making the MOSR bounce off any terrain it comes to contact with.


Code:
function Create(self)
   self.LTimer = Timer();
   self.hurtTimer = Timer();
   self.hurtInterval = 10;
   self.targethit = 0;
end

function Destroy(self)
end

function Update(self)
   if self.LTimer:IsPastSimMS(5) then
      for actor in MovableMan.Actors do
         if (math.abs(actor.Pos.X - self.Pos.X) < 15) and (math.abs(actor.Pos.Y - self.Pos.Y) < 15) and (actor.Mass <= 600) and (self.targethit == 0) then
         --Light up the candle and set various things to it.
            self.Lifetime = self.Lifetime + 100;
            actor:FlashWhite(1000);
            actor.HitsMOs = false;
            actor.GetsHitByMOs = false;
            actor.Pos = self.Pos;
            actor:SetAimAngle(math.random(math.pi/-2, math.pi/2));
            actor:SetControllerMode(Controller.CIM_DISABLED, -1);
            actor.Vel.Y = actor.Vel.Y * 0.3;
            actor.Vel.X = actor.Vel.X * 0.3;
            self.Vel.Y = self.Vel.Y * 0.8;
            self.Vel.X = self.Vel.X * 0.8;
            if self.hurtTimer:IsPastSimMS(self.hurtInterval) then
               actor.Health = actor.Health - 1;
               self.hurtTimer:Reset();
            end
            if actor.Health < 0 then
               --actor:GibThis() ;
               actor.Lifetime = 1;
               self:GibThis() ;
            end
         end   
      end
   end

   --I was intending to make the energy ball rebound at a random direction when it hits a surface, but I dont know how.
   --if (self.Vel.Y < 10) and (self.Vel.X < 10) then
   --   
   --end

   if self.LTimer:IsPastSimMS(5000) then
      self:GibThis() ;
   end
end


Last edited by numgun on Fri May 29, 2009 9:30 pm, edited 1 time in total.



Fri May 29, 2009 10:42 am
Profile
User avatar

Joined: Fri Jan 26, 2007 3:22 am
Posts: 1451
Reply with quote
Post Re: How to make my MOSR bounce? Energy ball!
numgun wrote:
I'm trying to create a gun that causes the same effect that halflife 2 had when someone was hit by one of those funny energy orbs. I have the gun and effects working and all, but I dont know how to make it bounce of terrain.

So all I need is the code for making the MOSR bounce off any terrain it comes to contact with.


Code:
function Create(self)
   self.LTimer = Timer();
   self.hurtTimer = Timer();
   self.hurtInterval = 10;
   self.targethit = 0;
end

function Destroy(self)
end

function Update(self)
   if self.LTimer:IsPastSimMS(5) then
      for actor in MovableMan.Actors do
         if (math.abs(actor.Pos.X - self.Pos.X) < 15) and (math.abs(actor.Pos.Y - self.Pos.Y) < 15) and (actor.Mass <= 600) and (self.targethit == 0) then
         --Light up the candle and set various things to it.
            self.Lifetime = self.Lifetime + 100;
            actor:FlashWhite(1000);
            actor.HitsMOs = false;
            actor.GetsHitByMOs = false;
            actor.Pos = self.Pos;
            actor:SetAimAngle(math.random(math.pi/-2, math.pi/2));
            actor:SetControllerMode(Controller.CIM_DISABLED, -1);
            actor.Vel.Y = actor.Vel.Y * 0.3;
            actor.Vel.X = actor.Vel.X * 0.3;
            self.Vel.Y = self.Vel.Y * 0.8;
            self.Vel.X = self.Vel.X * 0.8;
            if self.hurtTimer:IsPastSimMS(self.hurtInterval) then
               actor.Health = actor.Health - 1;
               self.hurtTimer:Reset();
            end
            if actor.Health < 0 then
               --actor:GibThis() ;
               actor.Lifetime = 1;
               self:GibThis() ;
            end
         end   
      end
   end

   --I was intending to make the energy ball rebound at a random direction when it hits a surface, but I dont know how.
   --if (self.Vel.Y < 10) and (self.Vel.X < 10) then
   --   
   --end

   if self.LTimer:IsPastSimMS(5000) then
      self:GibThis() ;
   end
end



uh there's the Bounce variable for materials

set it to 1


Fri May 29, 2009 1:36 pm
Profile

Joined: Sat Jan 13, 2007 11:04 pm
Posts: 2932
Reply with quote
Post Re: How to make my MOSR bounce? Energy ball!
No, not that bounce, the point is to keep the ball maintain the same speed it travels and even with bounce = 1, other materials that the ball will collide with will normally slow it down.

I need the object maintain its velocity always at 28 and bounce of any terrain surface it hits (or is close enough, 5 pixels), without losing any speed.


Fri May 29, 2009 2:05 pm
Profile
User avatar

Joined: Fri May 11, 2007 4:30 pm
Posts: 1040
Location: England
Reply with quote
Post Re: How to make my MOSR bounce? Energy ball!
well let's see... I'm not really sure if I have the right idea of how the ray functions work but I'd say run 2 CastStrengthRay's of (half your sprites width)+3 and make it skip all but the last pixel, in the direction depending on the velocity of the object (horizontal and vertical components, if positive X value then ray end pos = 10, if negative then -10). If the ray returns 1 then a terrain pixel was hit by the ray, if the X ray hit than reverse the X velocity and same with the Y.

see here for functions that you could use: http://www.datarealms.com/wiki/index.ph ... eneManager

I wish data would give examples, But I think you'd use it like so:
if CastStrengthRay(ball.Pos, (halfballwidthaddthree,0), 0, unnecessaryvector, halfballwidthaddtwo, 0, 1) == 1 do something
startpos, vector position of the end of the ray(unsure), check for pixels of strength grater than value, a vector to store the location of the pixel that was found (unnecessary for what you want to do but it must still be there), how many pixels to skip, ID of another material you want to skip(other than air, 0/default material because you don't really want to skip any other materials), wrap around map if crosses a seam(1, you want this).


Fri May 29, 2009 3:37 pm
Profile
User avatar

Joined: Mon Jun 30, 2008 9:13 pm
Posts: 499
Location: Finland
Reply with quote
Post Re: How to make my MOSR bounce? Energy ball!
Or you could set bounce to 1 and then set the vel to be 28 every frame. The bounce causes the proper direction change so it works.
Code:
function Update(self)
  local veldir = math.atan2(self.Vel.X,self.Vel.Y)
  self.Vel = Vector(28 * math.cos(veldir), 28 * math.sin(veldir))
end


Fri May 29, 2009 9:31 pm
Profile
User avatar

Joined: Fri May 11, 2007 4:30 pm
Posts: 1040
Location: England
Reply with quote
Post Re: How to make an object bounce off terrain?
That would definitely be easier, but he did say he doesn't want to merely use bounce (presumably because of the problem of it getting stuck).

If you wanted I could write up my lua only method into a script


Sat May 30, 2009 2:24 am
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post Re: How to make an object bounce off terrain?
Quote:
No, not that bounce, the point is to keep the ball maintain the same speed it travels and even with bounce = 1, other materials that the ball will collide with will normally slow it down.

I need the object maintain its velocity always at 28 and bounce of any terrain surface it hits (or is close enough, 5 pixels), without losing any speed.


He said that he didn't want it to lose velocity.

Oh man locking velocity sure won't make it lose velocity.


Sat May 30, 2009 5:07 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.117s | 15 Queries | GZIP : Off ]