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.
[03:39 pm]
Oh_Man -- how many trigs u got?
[09:34 am]
GGmano -- there must be a limit for one player on triggers but also a max trigger in total i think
[09:33 am]
GGmano -- after deleting alot og triggers i now have to reduce it even more
[09:33 am]
GGmano -- damn my file still too big but it doesnt crash only gives message that it cant save to disk,, freaking sad that i wanted to do more stuff but im limited here
[2022-6-29. : 8:18 am]
GGmano -- I would have released my New map yesterday if scmdraft didnt fail on saving the merged file
[2022-6-29. : 8:17 am]
GGmano -- Not cool when I made such Nice system and I could make it even better if more trigger allowed
[2022-6-29. : 8:16 am]
GGmano -- Damn now I have reduce trigger amount impossible to save fil
[2022-6-27. : 11:03 pm]
jjf28 -- GGmano
GGmano shouted: its not possible to move around a p12 owned preplaced a
when I tried - ya not possible
[2022-6-27. : 11:02 pm]
Zoan -- idk, maybe if you change a setting with euds you can do it. Something like the Unit List and Palletes thing or whatever it's called
[2022-6-27. : 9:42 pm]
GGmano -- i thought that would be easy but dont work on my test
Please log in to shout.


Members Online: KrayZee, Roy