ID:265754
 
Just a general question. Say I wanted to move an object at a certain angle(0-360). How would I go about doing this. I am not talking about tile based movement either, this is pixel based movement. Is there any sort of algorithm that can achieve this. If there isn't, could someone please explain how to go about doing this. Thanks.
It's basic Trigonometry. The cosine of angle Θ is equal to the x displacement on a unit circle (radius of 1), and the sine of angle Θ is equal to the y displacement on a unit circle. So if you multiply both of those values by the distance you want to move in direction Θ, you get the final position.

Here's an example:
obj/Balloon
New()
var/Angle = rand(0,360) //Choose a random degree to move in
var/Dist = rand(4,64) //Choose a random distance
pixel_x = cos(Angle)*Dist //Take the x coordinate from the unit circle at Angle, and multiply it by the Distance to move
pixel_y = sin(Angle)*Dist


Also, you should note that pixel_x and pixel_y will round off, so if you're making pixel-based movement, be sure to create your own variables to store the actual x and y pixel offsets, and then just set pixel_x and pixel_y to it.
In response to DarkCampainger
Theodis' AltMoveDemo also has this pre-made, if you want to look into using that.
Shadowdarke's Pixel Projectiles performs this task. :O
To expand on what DarkCampainger said;

Suppose you want to move a distance of 7 units at an angle of 42 degrees. Consider a right angled triangle that can be constructed to show this information:

http://members.byond.com/Abhishake/files/triangle.PNG

The hypoteneuse (the longest side in that triangle) is your displacement. Instead of trying to move a diagonal distance, we can split up our movement into horizontal and vertical components. The horizontal + vertical components of our displacement are represented by the other two sides of the triangle.

Let's try to find the horizontal component. You should know from some trigonometry that

cos A = adjacent/hypoteneuse (the adjacent is the side next to the angle).
adjacent = hypoteneuse * cos A
adjacent = 7 * cos 42 = 7cos42

Next the vertical component.

sin A = opposite/hypoteneuse (the opposite is the side opposite the angle, the only side we haven't used yet).

opposite = hypoteneuse * sin A
opposite = 7 * sin 42 = 7sin42

Right so we can label our triangle like so:

http://members.byond.com/Abhishake/files/triangle2.PNG

You can check the maths right now by using Pythagoras' theorem:

a^2 + b^2 = c^2
(7cos42)^2 + (7sin42)^2 = c^2
49(cos42)^2 + 49(sin42)^2 = c^2
49( (sin42)^2 + (cos42)^2 ) = c^2

Using the identity that (sina)^2 + (cosa)^2 = 1

49(1) = c^2
c^2 = 49
c = 7 as desired.

You don't really need to have understood that little proof if you don't know enough trigonometry yet, but yeah, it just shows that the triangle is labelled correctly.

BYOND has in-built variables called pixel_x and pixel_y. pixel_x controls your horizontal components and pixel_y controls your vertical components.

So when the player moves 7 units diagonally at an angle of 42 degrees we could do this:

pixel_x += 7*cos(42)
pixel_y += 7*sin(42)

You can generalise the result to:

pixel_x += r*cos(a)
pixel_y += r*sin(a)

Where r is the distance you want to move and a is the angle you want to do it at.

*Edit*

This is on the assumption that you want to move on a flat plane. If you want to move on an inclined plane, the trigonometry gets a bit more complicated. Ask if you do.
In response to Abhishake
Don't forget the obvious part with the hit detection and the modification of x and y variables. A pixel movement system is useless if you don't do those things. :P
In response to D4RK3 54B3R
That's not what he asked about. And writing about almost completely different topics results in very very long posts that nobody likes to read.
In response to Abhishake
I know. I was just pointing out something that he might have missed.
I've got some (quite likely hastily-pasted together) source code with pixel movement of circles here. Have a look through it if you want, it could help you get an understanding of how pixel movement works.

Keep in mind that a lot of that is very hacky and not great programming practice - it was for the Game In A Day competition.