ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey. I'm working on a destructable terrain program. Most of it is working however there is a problem that Im stumped on. How would you get the x and y of every point inside of a circle, using only the x,y of the center, and a radius?
Right now my explosions are a square (not very realistic). using this:
Well you have the general idea down as to how to destroy a circle. Now the problem is really a question of increasing how many points you destroy as you go about the circle. It is rather hard to guess what your radius field represents but i will assume it is not representing a pixel. Circles are essentially impossible to mimic in a game since you would need an infinite resolution and an infinite amount of deleted pixels to have a perfect circle. Rahter in games whe just delete enough pixels to give the illusion that there is a circle.
Well then you need to scrap all that code. As they say it is rather hard to make a circle with squares.
Now you really have a couple options.
1). Use a polar coordinate algorithm that uses sin/cos to draw a circle.
Something like:
dx = x + r.cos(theta)
dy = y + r.sin(theta)
where dx is the x coordinate to destroy, r is the radius and theta is the angle in radians
2). Use an algorithm such as Bresenham's to draw a circle.
x = r*cos(theta)
y = r*sin(theta)
where x is the coordinate to destroy, r is the radius and theta the angle in radians.
From the center point of your circle, we'll perform the destroys in 4 quarters, dividing the areas along the compass axis (eg. NSEW)
Eg bottom right quarter:
Start deleting pixels in a horizontal row from the center until you reach limit().
Then start on the next row, delete along until limit() which will be slightly less.
Then start on the next row, delete along until limit() which will have decreased greater than it did the first time.
Repeat until you're radius rows down/at the bottom of the final circle.
Limit() is a function which when given the row number and radius returns the horizontal distance from the center (eg distance along the current row) at which the edge of the circle is reached.
This can be calculated by the fact that the traced curved edge is sinusoidal/in this case follows the value of the second quarter of a sine curve, between 90 and 180 degrees. You'd just have to do a bit of division to get what fraction of the way down the radius each row is (equal to how far between the 90 and 180 limits we are), and probably use integer calculations so you're rounding to the nearest whole pixel.
Another suggestion (and it partly depends on your implementation, which sounds like a large bitmap if you are working on a per-pixel basis).
You could just generate one or more binary (or even greater) mask of whatever shape you want your explosions to look like and use that ('AND'ing with your terrain or whatever calculation you would like for more complicated effects).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.