Author Topic: How to get Y coordinate using coordinates x, y from terrain  (Read 934 times)

Offline Jirdus

  • byte
  • *
  • Posts: 2
    • View Profile
How to get Y coordinate using coordinates x, y from terrain
« on: April 10, 2012, 12:11:43 am »
What is the best solution for the movement of my character on the terrain? I need to get Y coordinate from the plane at x, y coordinates. Currently i solve it with calMinDistance in every frame but it's too slow. Any idea or example code, please? :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11745
    • View Profile
    • http://www.jpct.net
Re: How to get Y coordinate using coordinates x, y from terrain
« Reply #1 on: April 10, 2012, 12:37:07 pm »
Depending on the polygon count of your terrain, it might help to use an OcTree to speed this up...like so:

Code: [Select]
OcTree oc = new OcTree(terrain.getMesh(), 250, OcTree.MODE_OPTIMIZED);
terrain.setOcTree(oc);

oc.setCollisionUse(true);
oc.setRenderingUse(false);

I had the same problem with my RPG prototype. Without the octree, player (or entity)-terrain collisions are pretty slow. With the octree, it's fine for the player itself. If multiple enemies should be calculated too, it might become a bit too much for current devices. So i added another option that i'm using for enemies only...i sampled the terrain's heights at fixed intervals using calcMinDistance and stored that in a simple (but memory intense) data structure (currently 800*800 samples). This is basically a height map and it's used to calculate an enemy's current height based on the closest samples with some crude interpolation. It's not perfect, but it works good enough IMHO.

Offline Jirdus

  • byte
  • *
  • Posts: 2
    • View Profile
Re: How to get Y coordinate using coordinates x, y from terrain
« Reply #2 on: April 16, 2012, 09:29:59 pm »
I finally solved the problem. I had use a heightmap once created using calcMinDistance, which is sufficient for my needs.

Thanks for help