Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3508 Members - Latest Member: NevilleKemp

26. May 2012, 06:17:26 pm
Xith3D CommunityGeneral CategoryGeneral Discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)collision with the city
Pages: [1]
Print
Author Topic: collision with the city  (Read 316 times)
reubenjoeymawthoh
Just dropped in

Offline Offline

Posts: 5



View Profile Email
« on: 21. September 2011, 02:36:45 pm »

i created a city in cityengine and export it as .3ds. i want that a car.3ds model will detect collision with the objects in the city. how can i achieve that ??
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #1 on: 21. September 2011, 05:42:47 pm »

You need to traverse the city-model's graph and derive a physical object for very single node, that you want to collide with. IIRC there's either example code in the xith-tk for that or even the XPAL directly supports that. I'm sure, you'll find that Smiley.
Logged
reubenjoeymawthoh
Just dropped in

Offline Offline

Posts: 5



View Profile Email
« Reply #2 on: 07. November 2011, 03:24:08 pm »

This is what I did but but it didn't work :
Code:
public class CarSimulation extends Xith3DTest
{
    private static final Vector2f X_AXIS = new Vector2f( 1.0f, 0.0f );

    private FirstPersonInputHandler fpih;
    private Node fpihDebugNode;
    private Model avatar;
    private Model floorModel;
    private Transform avatarTG;
    private PhysicsEngine physEngine;
    private CollideableGroup collGroup;
    private Body b;
    private float speed = 1.5f;

   
   
    @Override
    public void onKeyPressed( KeyPressedEvent e, Key key )
    {
        switch ( key.getKeyID() )
        {
            case Y:
                fpih.flipMouseYAxis();
                break;

        }

    }

   
    @Override
    public void onKeyReleased( KeyReleasedEvent e, Key key )
    {
        switch ( key.getKeyID() )
        {
            case V:
                fpihDebugNode.setRenderable( !fpihDebugNode.isRenderable() );
                break;           
        }
    }

   
    @Override
    public void onKeyStateChanged( KeyStateEvent e, Key key, boolean state )
    {
        switch ( key.getKeyID() )
        {
            case LEFT_CONTROL:
                fpih.setSuspended( state );
                break;
        }
    }

    private void createAvatarCollider( final PhysicsEngine physEngine, FirstPersonInputHandler fpHandler, BranchGroup sceneBranch)
    {
        SimulationWorld world = physEngine.getSimulationEngine().newWorld();

        world.setGravity( 0f,0f,0f );
        b = world.newBody();
world.addBody(b );
        FPIHInputBindingsManager bindings = fpHandler.getBindingsManager();
        bindings.createDefaultBindings();
        bindings.bind( Keys.LEFT, FPIHInputAction.TURN_LEFT, InputBindingsSet.PRIMARY );
        bindings.bind( Keys.RIGHT, FPIHInputAction.TURN_RIGHT, InputBindingsSet.PRIMARY );
        fpHandler.setMouseSuspended( true );

        fpHandler.setPhysicsObject( new MyFPIHPhysics( 2.7f, 2.0f, new Vector3f( 0f, 1f, 0f ), physEngine, world, true, true) );

        fpihDebugNode = fpHandler.getPhysicsObject().getDebugNode();
        sceneBranch.addChild( fpihDebugNode );

        fpHandler.getPhysicsObject().setSlidingColliderCheckCallback( physEngine.getCollisionEngine(), collGroup );

    }

    private void loadAvatar( URL modelResource, String skin, FirstPersonInputHandler fpHandler, BranchGroup sceneBranch, PhysicsEngine physEngine ) throws Exception
    {
        this.avatar = ModelLoader.getInstance().loadModel( modelResource, skin, 0.04f );
        this.avatarTG = new Transform();

        avatarTG.getTransform().setIdentity();
        avatarTG.addRotationY( FastMath.PI_HALF );
        avatarTG.addChild( avatar );

        avatarTG.addChild( new AmbientLight( Colorf.GRAY50 ) );

        AvatarTransform at = new DefaultAvatarTransform( avatarTG, 0f, sceneBranch );
        fpHandler.addAvatar( at );
        fpHandler.setThirdPersonOffset( 0f, 0f, 7f );
        fpHandler.setMaxThirdPersonDistance( 10f );
        fpHandler.setMinThirdPersonDistance( 10f );
        fpHandler.setDiscreteThirdPersonStepSize( 0.5f );
        fpHandler.setMovementSpeedForward(speed);     

        createAvatarCollider( physEngine, fpHandler, sceneBranch);
       
    }

   private void createWorldColliders( final CollisionEngine collEngine, Shape3D[] m )
    {
        this.collGroup = collEngine.newGroup( "Simple" );

       
        for ( int i = 0; i < m.length; i++ )
        {
            Collideable modelcollider = collEngine.newTriMesh(m[i]);
            collGroup.addCollideable( modelcollider );
        }
    }

    private float getAngle2X( Vector2f vec )
    {
        if ( vec.getY() <= 0f )
            return ( vec.angle( X_AXIS ) );

        return ( FastMath.TWO_PI - X_AXIS.angle( vec ) );
    }

    private Model createFloor(URL modelResource, String skin)throws Exception
    {

        floorModel = ModelLoader.getInstance().loadModel( modelResource, skin, 0.04f );
        return(floorModel);

    }   

    private BranchGroup createSceneGraph( ResourceLocator resLoc, FirstPersonInputHandler fpHandler ) throws Exception
    {
        BranchGroup scene = new BranchGroup();

        scene.addChild( new DirectionalLight( true, Colorf.GRAY50, new Vector3f( 0.0f, -0.77f, -0.77f ) ) );

        Texture.setDefaultFilter( TextureFilter.TRILINEAR );

        final float ROOM_WIDTH  = 1024f;
        final float ROOM_DEPTH = 768f;

        Model model = createFloor(resLoc.getResource( "coolcity/cool.3ds" ),"marvin.pcx");
        scene.addChild(model);
        createWorldColliders( physEngine.getCollisionEngine(), model.getShapes()  );
        Texture.setDefaultFilter( TextureFilter.NICER );
        loadAvatar( resLoc.getResource( "mine/pajero.obj" ), "marvin.pcx", fpHandler, scene, physEngine );

        return ( scene );
    }

    private HUD createHUD( Sized2iRO canvasRes )
    {
        HUD hud = new HUD( canvasRes, 800f );
        DeviceComponent[] comps = new DeviceComponent[] { new MouseAxis( null, 'm', "FPS input" ), Keys.LEFT_CONTROL, Keys.V, Keys.Y };
        String[] boundActions = new String[] { "Control", "Get HUD mouse control", "Toggle FirstPersonInputHandler collider visualization", "Invert the mouse y-axis" };
        TestUtils.displayInputBindings( comps, boundActions, hud );

        return ( hud );
    }

    public CarSimulation( BasicApplicationArguments arguments, ResourceLocator rl, Xith3DEnvironment env, FirstPersonInputHandler fpih ) throws Throwable
    {
        super( arguments.getMaxFPS() );
        this.physEngine =  new JoodePhysicsEngine();
        env.getCanvas().clear();
        env.getView().setPosition( 150f, 0f, 350f );
        env.setPhysicsEngine( physEngine );
        env.addRenderPass( SkyBoxTest.createSkyBox( rl.getResource( "skyboxes/" ), "normal" ) );
        this.fpih = fpih;       
        env.addPerspectiveBranch( createSceneGraph( rl, this.fpih ) );
        env.addHUD( createHUD( arguments.getResolution() ) );
    }

   
}

please help
« Last Edit: 07. November 2011, 03:28:38 pm by reubenjoeymawthoh » Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic