About Store Forum Documentation Contact



Post Reply 
Raycasting question
Author Message
Aniketos Offline
Member

Post: #1
Raycasting question
I basically need to preform a straight line cast from vec a to vec b and get an *obj/chr reference to the object that got hit in between.

What would be the best way to do this code wise?

Never take life seriously. Nobody gets out alive anyway.
12-05-2010 04:19 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Dynad Offline
Member

Post: #2
RE: Raycasting question
Hey,

Ive a snippet that i wrote months ago so im not sure if you need to change of it since the updates:


~Dynad

PHP Code:
void Player::Shoot()
{
    
// calculate world position and direction vectors
    
Vec     posdir
    
ScreenToPosDir(Vec2(0,0),pos,dir);
    
PhysHit phys_hit
        
    
Bool temp=ctrl.actor.ray();
    
ctrl.actor.ray(false);

    
OrientP &head cskel.getPoint("head");
    
    if(
Physics.ray(head.pos,head.dir*D.viewRange(),&phys_hit))//, IndexToFlag(OBJ_CHR))) // if ray test hit an actor
    
{
        if(
phys_hit.obj// if the actor comes from an object
        
{
            if(
Player *plr=CAST(Player,phys_hit.obj))// if the object is an item
            
{
                
chatGui.ChatAddMessage(S+"Player hit");
                
plr->Hit();
            }
            else 
// the actor doesn't have an object set, so most probably it's a terrain actor
            
{
                
// add a decal to the world terrain
                
Game::World.terrainAddDecal(WHITE,*Materials("Decal/bullet.mtrl"),Matrix().setPosDir(phys_hit.plane.posphys_hit.plane.normal).scaleOrn(0.05f));
            }
        }
    }
    
ctrl.actor.ray(temp);


There is always evil somewhere, you just have to look for it properly.
12-05-2010 01:32 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Driklyn Offline
Member

Post: #3
RE: Raycasting question
Also, have you looked at "EsenthelEngineSDK\Tutorials\Source\Advanced\2 - Animation, Physics, Rendering\Physics\06 - Ray testing.cpp"? Just use phys_hit.obj to get the object that got hit.
12-06-2010 07:18 AM
Find all posts by this user Quote this message in a reply
Post Reply