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.
[05:19 pm]
NudeRaider -- when in doubt, Amazon
[05:11 pm]
Vrael -- hey does anyone know where I might be able to get a de-humidifer somewhere in the united kingdom?
[03:27 am]
m.0.n.3.y -- Maybe because it's an EUD map?
[03:27 am]
m.0.n.3.y -- Can't upload maps to the DB. Error says "The action you have performed caused an Error". Any word?
[2024-4-25. : 7:46 am]
RIVE -- :wob:
[2024-4-22. : 6:48 pm]
Ultraviolet -- :wob:
[2024-4-21. : 1:32 pm]
Oh_Man -- I will
[2024-4-20. : 11:29 pm]
Zoan -- Oh_Man
Oh_Man shouted: yeah i'm tryin to go through all the greatest hits and get the runs up on youtube so my senile ass can appreciate them more readily
You should do my Delirus map too; it's a little cocky to say but I still think it's actually just a good game lol
[2024-4-20. : 8:20 pm]
Ultraviolet -- Goons were functioning like stalkers, I think a valk was made into a banshee, all sorts of cool shit
[2024-4-20. : 8:20 pm]
Ultraviolet -- Oh wait, no I saw something else. It was more melee style, and guys were doing warpgate shit and morphing lings into banelings (Infested terran graphics)
Please log in to shout.


Members Online: jun3hong, NudeRaider