Raised This Month: $ Target: $400
 0% 

[TF2] Find intercept point of rocket and target


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Pelipoika
Veteran Member
Join Date: May 2012
Location: Inside
Old 10-01-2014 , 08:21   [TF2] Find intercept point of rocket and target
Reply With Quote #1

I've been trying to port this guys pseudocode @ http://gamedev.stackexchange.com/que...t-in-a-3d-game to work with sourcepawn and to support 3D space and heres what i have done.

PHP Code:
public OnEntityCreated(entity, const String:classname[])
{
    if (
StrEqual(classname"tf_projectile_rocket"))
    {
        
SDKHook(entitySDKHook_SpawnPostOnRocketSpawn);
    }
}

public 
OnRocketSpawn(rocket)
{
    if(
IsValidEntity(rocket))
    {
        new 
rOwner GetEntPropEnt(rocketProp_Data"m_hOwnerEntity");
        if(
IsValidClient(rOwner))
        {
            new 
target GetClosestPlayer(rOwner);
        
            
decl Float:vecTargetPos[3], Float:vecTargetVel[3], Float:vecShooterPos[3], Float:vecRocketVel[3], Float:vecToTarget[3];
            
            
GetEntPropVector(targetProp_Data"m_vecVelocity"vecTargetVel);
            
GetEntPropVector(rocketProp_Data"m_vecVelocity"vecRocketVel);
            
GetClientAbsOrigin(targetvecTargetPos);
            
GetClientAbsOrigin(rOwnervecShooterPos);
            
            
vecToTarget[0] = vecTargetPos[0] - vecShooterPos[0];
            
vecToTarget[1] = vecTargetPos[1] - vecShooterPos[1];
            
vecToTarget[2] = vecTargetPos[2] - vecShooterPos[2];
        
            new 
Float:GetVectorDotProduct(vecTargetVelvecTargetVel) - (vecRocketVel[0] * vecRocketVel[0] +
                                                                              
vecRocketVel[1] * vecRocketVel[1] + 
                                                                             
vecRocketVel[2] * vecRocketVel[2]);
            new 
Float:GetVectorDotProduct(vecTargetVelvecToTarget);
            new 
Float:GetVectorDotProduct(vecToTargetvecToTarget);

            new 
Float:= -/ (a);
            new 
Float:SquareRoot((b) - c) / (a);

            new 
Float:t1 q;
            new 
Float:t2 q;
            new 
Float:t;

            if (
t1 t2 && t2 0)
                
t2;
            else
                
t1;

            
decl Float:aimSpot[3];
            
aimSpot[0] = vecTargetPos[0] + vecTargetVel[0] * t;
            
aimSpot[1] = vecTargetPos[1] + vecTargetVel[1] * t;
            
aimSpot[1] = vecTargetPos[2] + vecTargetVel[2] * t;
            
            
PrintToServer("%f %f %f"aimSpot[0], aimSpot[1], aimSpot[2]);
            
            
decl Float:bulletPath[3];    
            
bulletPath[0] = aimSpot[0] - vecShooterPos[0];
            
bulletPath[1] = aimSpot[1] - vecShooterPos[1];
            
bulletPath[2] = aimSpot[2] - vecShooterPos[2];
        }
    }

I don't know whats wrong, if the target isn't moving at all it just prints NaN NaN NaN to the server console, otherwise it will just error with ""SquareRoot" reported: Cannot evaluate the square root of a negative number"

I am not a mathmagician
__________________
Pelipoika is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-02-2014 , 04:00   Re: [TF2] Find intercept point of rocket and target
Reply With Quote #2

Not a number

I ported this, if i remmeber And can find it, i'll paste. Iirc it only will predict the path to a moving target if the player firing the rocket is not moving. If you want that factored in, you will have to do some more maths.

I'm sure there is an internal function in the game somewhere, or a better way to do it, because bots use it.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-02-2014 , 14:03   Re: [TF2] Find intercept point of rocket and target
Reply With Quote #3

PHP Code:
//first-order intercept using absolute target position (http://wiki.unity3d.com/index.php/Calculating_Lead_For_Projectiles)
FirstOrderIntercept(Float:shooterPosition[3],Float:shooterVelocity[3],Float:shotSpeed,Float:targetPosition[3],Float:targetVelocity[3])
{
    
decl Float:targetRelativePosition[3];
    
SubtractVectors(targetPositionshooterPositiontargetRelativePosition);
    
decl Float:targetRelativeVelocity[3];
    
SubtractVectors(targetVelocityshooterVelocitytargetRelativeVelocity);
    new 
Float:FirstOrderInterceptTime(shotSpeedtargetRelativePositiontargetRelativeVelocity);

    
ScaleVector(targetRelativeVelocityt);
    
AddVectors(targetPositiontargetRelativeVelocitytargetPosition);
}

//first-order intercept using relative target position
Float:FirstOrderInterceptTime(Float:shotSpeedFloat:targetRelativePosition[3], Float:targetRelativeVelocity[3])
{
    new 
Float:velocitySquared GetVectorLength(targetRelativeVelocitytrue);
    if(
velocitySquared 0.001)
    {
        return 
0.0;
    }

    new 
Float:velocitySquared shotSpeed*shotSpeed;
    if (
FloatAbs(a) < 0.001)  //handle similar velocities
    
{
        new 
Float:= -GetVectorLength(targetRelativePositiontrue)/(2.0*GetVectorDotProduct(targetRelativeVelocitytargetRelativePosition));

        return 
0.0 0.0//don't shoot back in time
    
}

    new 
Float:2.0*GetVectorDotProduct(targetRelativeVelocitytargetRelativePosition);
    new 
Float:GetVectorLength(targetRelativePositiontrue);
    new 
Float:determinant b*4.0*a*c;

    if (
determinant 0.0)  //determinant > 0; two intercept paths (most common)
    

        new 
Float:t1 = (-SquareRoot(determinant))/(2.0*a);
        new 
Float:t2 = (-SquareRoot(determinant))/(2.0*a);
        if (
t1 0.0)
        {
            if (
t2 0.0
            {
                return 
t2 t2 t1 t2//both are positive
            
}
            else
            {
                return 
t1//only t1 is positive
            
}
        }
        else
        {
            return 
t2 0.0 t2 0.0//don't shoot back in time
        
}
    }
    else if (
determinant 0.0//determinant < 0; no intercept path
    
{
        return 
0.0;
    }
    else 
//determinant = 0; one intercept path, pretty much never happen
    
{
        
determinant = -b/(2.0*a);       // temp
        
return determinant 0.0 determinant 0.0//don't shoot back in time
    
}


for example, a rocket is 1100.0
FirstOrderIntercept(origin, originvel, 1000000.0, angles, targetvel);
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:02.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode