Move Object At An Angle
Author |
Message |
Jeffmagma
|
Posted: Tue Dec 22, 2015 11:32 am Post subject: Move Object At An Angle |
|
|
What is it you are trying to achieve?
I'm trying to move an object (or just a pixel) at an angle. If i divide it small enough to move 1 pixel at a time, it only goes in 1 direction, but for example if the line is 1 pixel to the right then 2 pixels up then 1 to the right and so on, I want the movement to follow that pattern
What is the problem you are having?
I'm not exactly sure how to calculate how much it moves
Describe what you have tried to solve this problem
I've tried to make it move 1 pixel at a time towards an angle, but it would only go in 1 direction (used to be in moveF)
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: | setscreen ("offscreenonly")
var chars : array char of boolean
var a : int := 90
var x : int := 1
var y : int := 2
var mx, my, mb : int
fcn radians (a : int) : real
result a * 0. 0174533
end radians
proc drawrect (var p1, p2, p3, p4 : array 1 .. * of int, color : 1 .. 255)
drawline (p1 (x ), p1 (y ), p2 (x ), p2 (y ), color)
drawline (p2 (x ), p2 (y ), p3 (x ), p3 (y ), color)
drawline (p3 (x ), p3 (y ), p4 (x ), p4 (y ), color)
drawline (p4 (x ), p4 (y ), p1 (x ), p1 (y ), color)
end drawrect
fcn trigRound (func : string, l : int, nt : boolean) : int
if func = "cos" and nt = true then
result round (cos (radians (a + 90)) * l )
elsif func = "cos" and nt = false then
result round (cos (radians (a )) * l )
elsif func = "sin" and nt = true then
result round (sin (radians (a + 90)) * l )
elsif func = "sin" and nt = false then
result round (sin (radians (a )) * l )
end if
end trigRound
proc moveF
end moveF
proc drawtilted (cx, cy, a : int, color : 1 .. 255, filled : boolean)
var point1, point2, point3, point4, diagTR, diagBL : array 1 .. 2 of int
diagTR (x ) := cx + trigRound ("cos", 20, false)
diagTR (y ) := cy + trigRound ("sin", 20, false)
diagBL (x ) := cx - trigRound ("cos", 20, false)
diagBL (y ) := cy - trigRound ("sin", 20, false)
point1 (x ) := diagTR (x ) - trigRound ("cos", 5, true)
point1 (y ) := diagTR (y ) - trigRound ("sin", 5, true)
point2 (x ) := diagTR (x ) + trigRound ("cos", 5, true)
point2 (y ) := diagTR (y ) + trigRound ("sin", 5, true)
point3 (x ) := diagBL (x ) + trigRound ("cos", 5, true)
point3 (y ) := diagBL (y ) + trigRound ("sin", 5, true)
point4 (x ) := diagBL (x ) - trigRound ("cos", 5, true)
point4 (y ) := diagBL (y ) - trigRound ("sin", 5, true)
drawrect (point1, point2, point3, point4, color)
if filled then
drawfill (cx, cy, brightred, color)
end if
end drawtilted
loop
Input.KeyDown (chars )
if chars (KEY_UP_ARROW) then
moveF
end if
drawtilted (320, 200, a, black, true)
put a : 3, " degrees"
View.Update
cls
delay (10)
mousewhere (mx, my, mb )
if mb = 1 then
a + = 1
if a >= 360 then
a - = 360
end if
end if
end loop
|
Please specify what version of Turing you are using
OpenTuring 1.0.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Tue Dec 22, 2015 2:12 pm Post subject: RE:Move Object At An Angle |
|
|
First of all, you can ditch your radians function by switching from sin/cos to sind/cosd. sind is sine, in degrees.
I don't have time right now to take a close look at your code, but I suspect that the issue has to do with when you round your variables off. You generally want to round at the last possible moment. All of your relevant variables should be of type real, even your x and y variables. |
|
|
|
|
![](images/spacer.gif) |
Jeffmagma
|
Posted: Tue Dec 22, 2015 2:33 pm Post subject: Re: Move Object At An Angle |
|
|
Insectoid wrote: First of all, you can ditch your radians function by switching from sin/cos to sind/cosd. sind is sine, in degrees.
Thanks for telling me about sind/cosd! I didn't know about those!
I'll try rounding a bit later and see if that works...
EDIT: Rounding at the last moment worked! Thanks for your advice! |
|
|
|
|
![](images/spacer.gif) |
|
|