Jump to content
  • Advertisement
Sign in to follow this  
mickeyren

generating a crater on the terrain

This topic is 3511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Generating a square crater is easy, I just specify a rectangle coordinates and I would update my height map table:
Lower( RECTANGLE r, float amount )
{
for(int y=r.top;y<=r.bottom;y++)
        for(int x=r.left;x<=r.right;x++)
	{
	      mheightTable[x + y * mTerrainSize.x] -= f;
        }
}


Unfortunately craters are circular and I could not get my head around getting all the coordinates that covers the entire circle surface. At first I tried to plot the points of a circle, but then I will just get the X,Y coordinate of the perimeter of a circle, I need to cover the whole area of the circle. Code would be lovely. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Hint: the points inside the circle will all be <= a certain distance (the circle radius) from the centre of your rectangle...

Share this post


Link to post
Share on other sites
knowing that the formula of a circle around origin is : x^2 + y^2 = radius

so have a test in your loop
if (x^2 + y^2) <= radiux) then your in your crater

you can now have a the height calculated on a function based on the distance to the origin.

hope that help

Share this post


Link to post
Share on other sites
a bit too advanced maybe, but for inspiration....

http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=263350

Share this post


Link to post
Share on other sites
hi guys

I think i may need a little more help.

Say my world has a height table of a size 200x200.

I create the terrain this big, scaled the x and z by 5.f;

There's a collision point returned at say - x = 50.f, z = 50.f.

I'd like to create a crater of radius 10.f;

So, would my steps be, loop through my whole height table that is,

for( int y = 0; y < size.y; y++ )
for( int x = 0; x < size.x; x++ )

Then what's next? where does the point and the radius logic comes in?

Thanks.




Share this post


Link to post
Share on other sites
Ok,

for( int y = 0; y < size.y; y++ )
for( int x = 0; x < size.x; x++ )


Loops through all your points in the heightmap. You want a crater cantered on one of those points with radius 10.0f; so you want to store the location of the crater, then check how far each point is from it (HINT: pythagoras).

If this distance is < radius, it forms part of the crater. How deep the point is should be based on how close the point is to the crater's center.

So if the point is really close, you make it deep. As the distance increases, the depth decreases.

Hope that makes sense.

Share this post


Link to post
Share on other sites
As you don't need to change anything outside of your crater i would do this kind of loop

RadiusPowerTwo = pow(radius,2);

For(int x = centre.x - radius; x < centre.x + radius; x++)
For(int y = centre.y - radius; y < centre.y + radius; y++)
{
if (pow(x,2) + pow(y,2) <= RadiusPowerTwo)
{
//you are in the circle, what distance are you from the centre?
distance = sqrt(pow(x,2) + pow(y,2));
height[x][y] = transform(distance);
}
}

the transform method get a distance and tell what the height should be according to that distance from the centre.

Share this post


Link to post
Share on other sites
Hi guys

Thanks for trying to help.

mcmuzzle:

I'm not sure, the code does not seem to look right. Its a circle, would the loop plot all the points of a circle?

I picture it that it would leave a quadrant out.

Let me try.

*edit - i tried, and actually the opposite happened, it only modified a quarter of a circle.

This was my original code:

for(int i=0; i <= 360; i++)
{
//Calculate theta for this vertex
float Theta = D3DXToRadian(i);

//Compute X and Y locations
int X= (float)(pt.x + radius * cos(Theta));
int Y = (float)(pt.y - radius * sin(Theta));

m_heightTable[X + Y * m_ptSize.x] = -1.f;
}



But this one just plots the perimeter. Could not get my head around covering the whole area.

[Edited by - mickeyren on December 8, 2008 11:50:00 AM]

Share this post


Link to post
Share on other sites
Use the method mcmuzzle suggested; the code seems pretty much fine.

You'll only be getting a quadrant done as you need the distance to always be positive. try distance = fabs(sqrt(pow(x,2) + pow(y,2))); instead.


Share this post


Link to post
Share on other sites
Hi brynbryn

If I want to create a crater at some far away point from the origin say 30,30 and a small radius say 5

This will never be satisfied:

if (pow(x,2) + pow(y,2) <= RadiusPowerTwo)

because RadiusPowerTwo is only 25.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!