Announcement

Collapse
No announcement yet.

Calculating hit reflected direction

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • replied
    Try this code, it works fine with my game type and I can trace all walls with normals and projection.
    Didn't put much effort to style or optimize it, but I hope you find some use for it.

    Code:
    local Actor HitActor;
    	local Vector HitNormal, HitLocation, End, Start, pEnd;
    	local float VectorSize;
    	local byte r,g,b;
    	VectorSize = 600.0;
    	r = 0;
    	g = 0;
    	b = 255;
    	// pawn view vector (blue)
    	Start = Pawn.Location + Normal(Vector(Pawn.Rotation)) * Pawn.GetCollisionRadius()/2;
    	End = Pawn.Location + Normal(Vector(Pawn.Rotation)) * VectorSize * 3;
    	HitActor = Trace(HitLocation, HitNormal, End, Start, true,);
    	pEnd = End;
    	if(HitActor != none)
    	{
    		if (HitActor == WorldInfo || HitActor.isA('StaticMeshActor'))
    		{
    			g = 255;
    			b = 0;
    		}
    		// end of sight
    		myHUD.Draw3DLine(Start, HitLocation, MakeColor(r,g,b,255));
    		// draw recoil
    		Start = MirrorVectorByNormal(vector(Pawn.Rotation), HitNormal);
    		End = HitLocation + Normal( Start)*VectorSize/2;
    		myHUD.Draw3DLine(HitLocation, End, MakeColor(0,0,190,255));
    		// draw the normal
    		Start = HitLocation;
    		End = HitLocation + HitNormal*VectorSize/6;
    		myHUD.Draw3DLine(Start, End, MakeColor(255,0,0,255));
    	}
    	else
    	{
    		// draw end of sight line if nothing was hit
    		myHUD.Draw3DLine(Start, pEnd, MakeColor(0,0,255,255));
    	}
    This will give you the direction to bounce off after the trace.
    Code:
    Normal(MirrorVectorByNormal(vector(Pawn.Rotation), HitNormal))
    If someone has a better solution, please share some code.

    Leave a comment:


  • replied
    thanks guys, i will test all suggestions when i find some time to mess with udk again

    Leave a comment:


  • replied
    Depending on how many times you do this, you may just want to use the half normal between the hit normal & surface normal (same thing that is used for specular reflection.) This is cheaper to calculate than the reflection vector. I don't know how much faster it will do so in UScript, though... They kind of put you in a bind by having limited native functions.

    Leave a comment:


  • replied
    I have done something like this before, and I remember theres a function in the object class that helps you do it. don't have UDK with me right now, try searching for something along the lines of mirror vector by normal. will update with the exact function later if u need.

    EDIT: There you go.
    Code:
    native(300) static final function vector MirrorVectorByNormal( vector InVect, vector InNormal );

    Leave a comment:


  • replied
    I used a reflection vector to do a wall jump too. Have a look:

    Code:
    function DoReturnJump(Vector HitNormal) 
    {
    	local Vector vv; 
    	
    	vv = self.Pawn.Velocity;  
    
    	vv =  (vv - 2 * HitNormal * (vv dot HitNormal)  ) ; // Reflection vector
    
    	vv.Z = 200; //I want the reflected Z component to be fixed at 200
    
    	self.Pawn.Velocity = vv; // That's it	
    
    	rReturnJumpView = rotator( Normal(vv) );
    
    	rReturnJumpView.Pitch = 0; 
    
    	bPerformReturnJumpViewRotation = true;	
    }

    Leave a comment:


  • replied
    Originally posted by Saishy View Post
    That script will also not work correctly (your pawn will not face away from the wall) because newRotation is not a valid variable. That is a valid question, "where I paste the script" is not.

    I could keep ignoring it but some guy created 3 threads asking the same thing.
    it's all quite simple to get it working after that really... if you kinda know what you're doing
    anyway I did answer the question "where to paste it" in one of his threads. so this goes way beyond learning to script, and into learning to listen

    Leave a comment:


  • replied
    Agree with that. My advanced math skills are terrible, but have been coming up some since I've started doing this game stuff

    Leave a comment:


  • replied
    Actually, in all fairness, even if math is not your strong side, this should be a 5 second search in google along the lines of "calculating reflection vector".

    If you want to do any sort of programming, be it gameplay, graphics or something else, you will need to know a basic amount of math, and vectors are basic knowledge.

    Leave a comment:


  • replied
    Just saying I'm not answering any "What I do with this?" questions, if you don't even understand that, you will come here asking again every time you have a problem, and believe me, you will have a LOTS of problems.

    So, If you don't know what to do with that it means you are not ready to do any script at all. Start by reading the documentation and only post in the forum again when you are able to understand what is going on that script. (It is a very basic script)

    That script will also not work correctly (your pawn will not face away from the wall) because newRotation is not a valid variable. That is a valid question, "where I paste the script" is not.

    I could keep ignoring it but some guy created 3 threads asking the same thing.

    Leave a comment:


  • replied
    where should i paste this code???

    Leave a comment:


  • replied
    Now where would I place this code?

    Leave a comment:


  • replied
    Angle of incidence = angle of reflection

    Leave a comment:


  • replied
    My walljump:

    Code:
    function bool DoWallJump( bool bUpdating )
    {
    	local Vector HitLocation, HitNormal, End, Start;
    	local Actor HitActor;
    	Start = Location + (Vector(Rotation) * GetCollisionRadius()/2);
    	End = Start + (vector(Rotation) * GetCollisionRadius() * 1.5);
    	HitActor = Trace(HitLocation, HitNormal, End, Start, false,);
    	if (HitActor == WorldInfo)
    	{
    		if (HitNormal.X != 0)
    		{
    			Velocity.X = HitNormal.X * WallJumpBoostXY;
    		}
    
    		if (HitNormal.Y != 0)
    		{
    			Velocity.Y = HitNormal.Y * WallJumpBoostXY;
    		}
    
    		newRotation = Rotator(Velocity);
    		Velocity.Z = JumpZ + WallJumpBoostZ;
    		MultiJumpRemaining = 0;
    		bWallJump = true;
    		//CustomGravityScaling = 0.9f;
    		AirControl = WallJumpAirControl;
    
    		return true;
    	}
    	return false;
    }
    The logic is, you don't need to change the horizontal momentum because it is going where you want, just making it jump against the wall will give you your desired effect.

    Leave a comment:


  • replied
    Try using tracehit to get location and send the projector back but with a bit of random offset.

    Leave a comment:


  • started a topic Calculating hit reflected direction

    Calculating hit reflected direction

    Can somebody help me with calculating reflected direction of bullet hitting the wall ?

    what i mean :


    math was never my strong point,
    i basically need it for spawning impact effect at appropriate angle (inside class WeaponAttachment.PlayImpactEffects)

    edit: i need to add its for multiplayer game
Working...
X