It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm a complete newb, working on a 2D RTS game. So far I've created an object "infantry" and can make it shoot a "bullet" in specified direction, and can only find the code to delete bullet once outside the room. I want the bullet to delete at a relative distance of 300pixels from the infantry. And need the code to be very clean, hence there will be a lot of bullets eventually. My question is: Should I make a rectangle around the infantry, to specify range and line of sight, and when bullet collides with range = delete? or Should the bullet class say "once created + distance traveled = 300 pixels = delete"?... Any code, or hints you can offer would be greatly appreciated.
Simple method:
Store the origin point, where the projectile is created (at the gun barrel)
Each frame, use the distance formula to find out whether the projectile has exceeded the travel radius.
You can avoid the Math.Sqrt call in the distance formula, by comparing to the square of the radius.
var dx = xbullet - xorigin;
var dy = ybullet - yorigin;
if (dx*dx + dy*dy > range*range) delete bullet
I think your second suggestion is better. If you make a box or check the distance to the soldier, then the bullet could travel longer if the soldier run after the bullet (if the bullet is not instant-travel, that is).
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Is there a quick method to determine if an image is a square or not in C#?
Question has been phrased incorrectly, my apologies.
Is there a way to determine if an image can be scaled "down" to fit into a square block, without cropping either height or width, for example, if I have 960x640, we have a square, on it's width, but if we have 640x960, we don't.
I need to be able to determine if an image can be scaled down 100% into a square block, for example, 150x150, without losing portions of the image.
Update
Let me try again.
I have to iterate a collection of images:
960x658
960x566
960x381
960x378
714x960
658x960
I know, that the first two images will be square (150x150), I know the middle two will be rectangular (horizontal) (300x150) and I know the remaining two will be rectangular (vertical) (150x300). Is there an algorithm, 3rd party component or built in method to determine this for me?
I don't want to go and code nested spaghetti code using if statements to do this? I'm so lost :$
Is there a quick method to determine if an image is a square or not in C#?
Well you're pretty much giving the answer yourself. You have the image. You have the image's properties.
if(img.Width == img.Height)
//I'm a square
Now since you're iterating through a collection of images.
foreach(Image img in myImageCollection)
if(img.Width == img.Height)
squareImages.Add(img);
As simple as that.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to solve the Project Euler problem number 6 with the following program:
double counter = 1;
double sumsquare = 0;
double squaresum = 0;
while (counter <= 100)
{
sumsquare += Math.Pow(counter, 2);
squaresum += counter;
counter++;
}
Math.Pow(squaresum, 2);
Console.WriteLine("the sum is {0} ,the square is :{1}", squaresum.ToString(), sumsquare.ToString());
double diff = sumsquare - squaresum;
Console.WriteLine(diff.ToString());
Console.ReadLine();
However, the answer was wrong. Can anyone tell me what the problem is with my code?
You have an error in that you are computing a power and then discarding it.
More generally, you should avoid double when solving PE problems that involve integers. Doubles are only accurate to fifteen decimal places; after that, they round off.
Better types to use are long, which can represent integers up to 9,223,372,036,854,775,807 or decimal, which can represent integers up to 79,228,162,514,264,337,593,543,950,335, or BigInteger, which can represent arbitrarily large values.
Note that code typically gets slower as you use the larger and larger types.
Even more generally, now would be a good time to learn how to use a debugger. Figure out what the output of your program should be for a very small case that you can calculate by hand. Then step through your program line by line and see where it goes wrong. That is a far more efficient way to debug a problem than asking the internet.
You seem to think that Math.Pow(squaresum, 2); modifies squaresum. That's not so: it returns a square of squaresum, and you should assign it to squaresum if you want the variable to be modified.
squaresum = Math.Pow(squaresum, 2);
Also the idea of using double type here is not quite good, but it appears that you won't lose enough precision here to get a wrong result.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a FlowDocument in which I need to insert many fragments of dynamic text at arbitrary positions.
For example, I need to place text "Hello" at x = 10, y = 15 and text "World" at x = 10, y = 20.
I'm currently doing this using Figure. I create a Run with the text, put it in a Paragraph, and put all that in a Figure. From there I can set it's VerticalOffset and HorizontalOffset. Then I place all the figures in a Paragraph which I add to the FlowDocument.
Everything was going OK (it was the simplest approach I could came up) until I had to place two or three fragments of text on the same line (at the same y but different x).
For some reason it's putting each Figure on a new line instead of putting them all in the same line, let me illustrate:
Expected:
text1 text2 text3
Actual:
text1 text2 text3
Does anybody know how to remove that line break between figures? If you have a better approach to this problem I'm open to suggestions too.
I ditched FlowDocument and went with XAML and used a Canvas... made my life much easier being able to place all text fields visually... much easier to maintain in the long run too.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an application which uses the Microsoft Kinect camera device.
At each point I can obtain the position of my hand in the 3 Dimensional space ( X - Y - Z ) and I want to compute acceleration of my hand over each second on each axis.
Basically, I have the coordinates of a start point and also for after a second from that start point, and I want to compute the acceleration of my hand between those 2 points.
StartPoint - (x1, y1, z1)
EndPoint after 1 sec from StartPoint ( 30 frames ) - (x2, y2, z2)
Acceleration between StartPoint and EndPoint = ?
Also I can obtain all the other coordinates of my hand over time, but I want to compute the acceleration in the period of time between start point and end point.
Could you please explain or show me how?
The distance from StartPoint to EndPoint is a vector with 3 values, and it can gives you the speed (distance unity/second)
velocity(EndPoint.X - StartPoint.X, EndPoint.Y - StartPoint.Y, EndPoint.Z - StartPoint.Z)
Now, if you want the acceleration, you'll have to do the same with two velocity values: The velocity at the startpoint, and the velocity one second later.
acceleration(EndVelocity.X - StartVelocity.X, EndVelocity.Y - StartVelocity.Y, EndVelocity.Z - StartVelocity.Z)
acceleration represents the acceleration for each axes (X, Y and Z) and is expressed in (distance unity/secondĀ²)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want to create a simple game .
think I have two uneven ( not systematical ) GDI object in one Graphics , user move object one , and I want to be notified when object one has Collision with object two , how can I do this with C# ?
Draw both the objects black on a single bitmap. Then draw one of them on a separate bitmap. Then subtract one bitmap from the other. If the result is same as the bitmap with the other object drawn on a separate bitmap then you have no collision otherwise you have a collision.
This doesn't require any math and works with objects of irregular shape. Just bitmap difference and sum operation.
Sudo code
var bmpObject1 = new bitmap->draw object 1
var bmpObject2 = new bitmap>draw object 2
var bmpCombined = new bitmap>draw object 1 and 2
if (bmpObject1 = (bmpCombined - bmpObject2))
no collision
else
collision