Sign in to follow this  

generating a crater on the terrain

This topic is 3294 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
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
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
Quote:
Original post by mickeyren
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)
Probably a typo on mcmuzzle's part, but easy to correct:
radiusSquared = radius*radius;

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

Share this post


Link to post
Share on other sites
Hi swiftcoder

Thanks for trying to help, unfortunately it still does not create a full circle. It leaves part of the crater with straight edges.


Please see screen shot:



Wire frame:



I created bumps instead of crater to have a better visual on the computed circle.

We can see here that it forms straight lines along the edges.

I must be missing something. (I'm thinking that's as far as it can go, if I want a semi-perfect circle, then I need a bigger radius)

Thanks.

[Edited by - mickeyren on December 9, 2008 9:54:24 PM]

Share this post


Link to post
Share on other sites

This topic is 3294 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this