Author Topic: Polygon picking?  (Read 5712 times)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Polygon picking?
« Reply #15 on: February 17, 2011, 12:13:24 pm »
You can use the same collision detection approach that are using for picking to detect the height of the terrain simply by shooting a ray from above down to the terrain and calculate the intersection point. Alien Runner does this for example. That's what i would do with a terrain on the desktop. On Android, it might be a better idea to keep some simplified data structure storing the average height of a "tile" of the terrain and interpolate between those. It's not as accurate though, but should be much faster.

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Polygon picking?
« Reply #16 on: February 17, 2011, 08:29:40 pm »
I'm not sure the procedure you used to create your terrain, so this might not apply, but in my RobotBuilder project, I have the terrain built as a tiled grid with a height for each vertice.  I can accurately know the height at a given 2D coordinate on the map by interpolating between the two horizontal and two vertical vertices, as long as the remaining "catty-corner" vertice isn't completely different than the other three (which I've made sure of when I originally created the terrain).  I can provide some example code if that is hard to visualize from my description.

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Polygon picking?
« Reply #17 on: February 17, 2011, 09:27:17 pm »
I used my heightmap2serialized object generator :p (current name terragen :-\ ) to create the terrain,
some example code would not hurt (I even prefer getting some :) )

Terragen download (source)
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Polygon picking?
« Reply #18 on: February 19, 2011, 04:39:09 pm »
I still dont got this 'waling towards' a vector to work.
and have no clue on how to do this ..  :( would prefer an example ... :)
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Polygon picking?
« Reply #19 on: February 22, 2011, 11:30:23 am »
Finally figured it out.. Is is being done using Lerp

Code: [Select]
float speed = (0.5f * ticks);
float timeToGetThere = 1.0f / playerPos.distance(gotoPos) * speed;

playerPos = lerp(playerPos, gotoPos, timeToGetThere);
player.clearTranslation();
player.translate(playerPos);

Lerp function:
Code: [Select]
private SimpleVector lerp(SimpleVector start, SimpleVector end, double time){

float x = (float) (start.x + (end.x - start.x) * time);
float y = (float) (start.y + (end.y - start.y) * time);
float z = (float) (start.z + (end.z - start.z) * time);

return new SimpleVector(x, y, z);

}

What I still need though is to get the height based on terrain... at playerPos.x, playerPos.z
« Last Edit: February 22, 2011, 11:32:15 am by Kaiidyn »
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Polygon picking?
« Reply #20 on: February 27, 2011, 10:48:17 pm »
The direction vector in
Code: [Select]
this.terrain.calcMinDistance(SimpleVector.create(x,y,z), direction, 100);to cast this down, should it be 0,1,0?
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: Polygon picking?
« Reply #21 on: February 27, 2011, 10:48:56 pm »
Yes.

Offline Kaiidyn

  • long
  • ***
  • Posts: 103
    • View Profile
Re: Polygon picking?
« Reply #22 on: February 27, 2011, 10:49:57 pm »
wow, that was fast :p lol ok thanks :)


Got my getTerrainHeightAtXZ function finally working now :D
Code: [Select]
public float getTerrainHeightAtXZ(float x, float y, float z){

direction = SimpleVector.create(0,1,0);
distance = this.terrain.calcMinDistance(SimpleVector.create(x,y,z), direction, 1000);

if(distance != Object3D.COLLISION_NONE){

collisionPoint = direction;
        collisionPoint.scalarMul( distance );
        collisionPoint.add( SimpleVector.create(x,y,z) );
        Log.d(collisionPoint.y + "");
        return collisionPoint.y;

}


return 0;
}

Thanks for all the help :)
« Last Edit: February 27, 2011, 10:54:38 pm by Kaiidyn »
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control. - Grady Booch