Advertisement

SDL Angular Movement

Started by August 19, 2011 10:57 PM
2 comments, last by burgero 13 years, 3 months ago
Hello,

I'm new to game programming and I am currently working a 2D Sidescroller using SDL. I'm attempting to make a function that will move an object from point A to point B in a straight line at a fixed speed...so if the speed is set to 5 then it will move 5 pixels across the line each frame.

Things are looking pretty good, but I am running into an issue if the slope of the line is extreme. If I have a point located at (0,0) and another located at (3,30), and I want to move at a speed of 5 pixels a frame. The issue I have is that because I can't do decimals in SDL_Rect I get a sort of staircase like movement. Does anyone have any suggestions to keep the movement in a smooth straight line, or am I out of luck?
Easiest way is probably to store the positions as floating point numbers (float or double).
Advertisement

Easiest way is probably to store the positions as floating point numbers (float or double).

It would not help if you don't turn on multi-sampling, but it is not that which is the problem. You should use floats to calculate the vector between the points, then convert the numbers to int, if you cast it would round off to the lowest nearest number. It may not be what you want. Use ceil function if you want to round up or use floor to round down.
I got it to look a lot better, thanks. For all my calculations I made sure to store the points as floats, and then right I showed the image I casted them to ints.

Another issue I was having was with my calculations....Every frame the equation of the line to move on was re-calculating, which was causing some bad looking movement. I made sure that my image moved along the same line until it reached its destination, and this made things look a lot better.

Thanks for your replies!

This topic is closed to new replies.

Advertisement