C# help
Jan 24 2008, 8:53 pm
By: frazz  

Jan 24 2008, 8:53 pm frazz Post #1



Quote
if (countFrames > 0)
{
bullet.X = (int)((double)bullet.X - ((double)bullet.X - (double)bulletD.X) / (double)countFrames);
bullet.Y = (int)((double)bullet.Y - ((double)bullet.Y - (double)bulletD.Y) / (double)countFrames);
countFrames--;
}
else if (countFrames == 0)
{
bullet.X = (int)((double)bullet.X - ((double)bulletStart.X - (double)bulletD.X) / (double)countFramesInit);
bullet.Y = (int)((double)bullet.Y - ((double)bulletStart.Y - (double)bulletD.Y) / (double)countFramesInit);
countFrames--;
}
else
{
bullet.X = (int)((double)bullet.X - ((double)bullet.X - (double)bulletD.X) / (double)countFrames);
bullet.Y = (int)((double)bullet.Y - ((double)bullet.Y - (double)bulletD.Y) / (double)countFrames);
countFrames--;
}
This is an algorithm for moving a bullet from the right side of the screen to the left. The problem is the bullet moves in a wavy pattern. I would expect this when countFrames is 0 or less, but it happens before then. The bullet typically moves about halfway, then turns, but always hits the spot at bulletD. Can anyone suggest a better algorithm?



None.

Jan 25 2008, 6:56 pm fatimid08 Post #2



The problem here is precision loss I think. By casting the double to an int, you lose everything after the decimal point, plus I think the cast also rounds up or down depending on what was there. That can cause what looks like a straight line in code to be not so straight in appearance.

First thing to try is to use integers only (complete with integer division and all), unless you already tried it and it didn't work.

Else, C# can use the single datatype for drawing, allowing you to keep one decimal. I suggest that if any of calculations used for positions on the screen which give decimals, they should be done with the single datatype (and PointF, SizeF, RectangleF).

Also, you are casting a bit too much. ((double)bullet.Y - (double)bulletD.Y) can be simplified to (double)(bullet.Y - bulletD.Y) if you really need the cast. You don't need to cast countFrames to double because if one operand is a double (and it is because you cast it), the other is automatically cast by the runtime anyways, so you can just remove it and improve readability a bit.

Also, I can't see the difference between the if clause and the else one, so you could put countFrames != 0 as condition and remove the else altogether, and change the elseif to else.



None.

Jan 25 2008, 10:41 pm Darkangel123 Post #3



I would probably be able to help if you were addressing C++. But you're not, so I might as well not even have posted. In fact, I didn't even post. I just left this in my message box, hoping no one would click it.

If you're seeing this, then someone clicked it.



None.

Jan 28 2008, 4:54 am cheeze Post #4



Can you explain what all the variables represent before expecting a helpful reply? (CEPT THIS ONE CUZ I'm Awesome like that)



None.

Jan 28 2008, 7:16 am Centreri Post #5

Relatively ancient and inactive

I don't know C, but this particular script is remarkably similar to it's PHP counterpart. I don't understand what all the variables mean.. but why are you changing the Y value at all? Only one value needs to be changed, unless C# has some weird tilted axis. Depending on exactly what's happening here (variables, all that - is it in progressive order? Frame 1,2,3, or whatever?), can't you just make a loop so that for every integer i, the bullet's position is x*i? Where do ifs come into this?

Basically, more information. I'm probably horrifically wrong because I didn't see the big picture.



None.

Feb 21 2008, 6:20 pm frazz Post #6



Sorry I was kinda in a hurry when I posted this. I thought the variables were mostly self explanatory. Anyway, my final's over, but I started another program and got it working a bit better.
Thanks for the help everybody who helped. Thing is I though I checked a few days after and didn't see any replies.

Using all ints shoots the entire thing to nowhere. You get bullets going all over the place, regardless of where you click.

Here's what I ended up with.
Quote
if (countFrames > 0)
{
bulletXSpeed = (-((double)bullet.X - bulletDX) / (double)countFrames);
bulletYSpeed = (-((double)bullet.Y - bulletDY) / (double)countFrames);
bullet.X = bullet.X + (int)bulletXSpeed;
bullet.Y = bullet.Y + (int)bulletYSpeed;
countFrames--;
}

else if (countFrames == 0)
{
bullet.X = bullet.X + (int)bulletXSpeed;
bullet.Y = bullet.Y + (int)bulletYSpeed;
countFrames--;
}
else
{
bulletXSpeed = (-((double)bullet.X - bulletDX) / (double)countFrames);
bulletYSpeed = (-((double)bullet.Y - bulletDY) / (double)countFrames);
bullet.X = bullet.X + (int)bulletXSpeed;
bullet.Y = bullet.Y + (int)bulletYSpeed;
countFrames--;
}
This worked better, but if I make countFrames too small it messes up. This essentially limits the speed of my bullet, and it's not perfect anyway. Plus this whole thing is a pain to read, and probably wastes processing space.

My newer program uses this beautiful chunk of code.
Quote
bullet.X = bulletStart.X + (int)(bulletXSpeed * (double)(countFramesInit - countFrames));
bullet.Y = bulletStart.Y + (int)(bulletYSpeed * (double)(countFramesInit - countFrames));
countFrames--;
I love it, this allows for great accuracy, regardless of speed. There's no drifting too. I don't know why I couldn't figure this out a long time ago.

Thanks for the help guys, unless somebody has a better idea.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[11:50 pm]
O)FaRTy1billion[MM] -- nice, now i have more than enough
[11:49 pm]
O)FaRTy1billion[MM] -- if i don't gamble them away first
[11:49 pm]
O)FaRTy1billion[MM] -- o, due to a donation i now have enough minerals to send you minerals
[03:26 am]
O)FaRTy1billion[MM] -- i have to ask for minerals first tho cuz i don't have enough to send
[2024-4-17. : 1:53 am]
Vrael -- bet u'll ask for my minerals first and then just send me some lousy vespene gas instead
[2024-4-17. : 1:52 am]
Vrael -- hah do you think I was born yesterday?
[2024-4-17. : 1:08 am]
O)FaRTy1billion[MM] -- i'll trade you mineral counts
[2024-4-16. : 5:05 pm]
Vrael -- Its simple, just send all minerals to Vrael until you have 0 minerals then your account is gone
[2024-4-16. : 4:31 pm]
Zoan -- where's the option to delete my account
[2024-4-16. : 4:30 pm]
Zoan -- goodbye forever
Please log in to shout.


Members Online: Roy, Ultraviolet, RIVE