Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 angle problem
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Fri Dec 08, 2006 10:13 am   Post subject: angle problem

code:
setscreen ("offscreenonly")
var angle : int := 1
var velx, vely := 0
var chars : array char of boolean
var pic : array 1 .. 359 of int
var shipx, shipy : int := 100
var linex1, liney1, linex2, liney2 : int
var x, y, button : int
linex1 := shipx + 23
linex2 := shipx + 23
liney1 := shipy + 38
liney2 := shipy + 28

for i : 1 .. 359
    pic (i) := Pic.FileNew ("ship.bmp")
    pic (i) := Pic.Rotate (pic (i), i, 23, 33)
    drawfillbox (10, 200, (i + 10), 250, 12)
    locate (12, 48)
    put "Complete"
    View.Update
    locate (14, 18)
    put "GENERATING PICTURES"
end for

loop
    Input.KeyDown (chars)
    mousewhere (x, y, button)
    View.Update
    delay (30)
    cls
    Pic.Draw (pic (angle), shipx, shipy, picCopy)
    drawline (linex1, liney1, linex2, liney2, 7)
    if x = 0 then
        x := 1
    end if
    if y = 0 then
        y := 1
    end if
    shipx += velx
    shipy += vely
    angle := arctan (y div x) div 1
    if chars (KEY_UP_ARROW) then
        velx := (linex1 - linex2) div 5
        vely := (liney1 - liney2) div 5
    end if
    if button = 1 then
        velx := (x - shipx) div 20
        vely := (y - shipy) div 20
    end if
    if angle >= 360 then
        angle := 1
    end if
    if angle < 1 then
        angle := 358
    end if
    locate (1, 1)
    put "Angle: ", angle
    locate (2, 1)
    put "x: ", x, "    y: ", y
end loop


If you click, the ship moves properly, but can anyone help me with the formula for the way the ship is facing?
I want it to face the mouse, but I have tried a lot of different ways and i don't know how to do it. Anyone know the formula?

ps. Code is messy, but don't worry about that



ship.bmp
 Description:
 Filesize:  9.33 KB
 Viewed:  813 Time(s)

ship.bmp


Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Fri Dec 08, 2006 3:56 pm   Post subject: (No subject)

this might help: http://www.compsci.ca/v2/viewtopic.php?t=14083
richcash




PostPosted: Mon Dec 11, 2006 2:44 pm   Post subject: (No subject)

I assume you mean something like this :
code:
View.Set ("offscreenonly")
var x1, y1, x2, y2, angle : real := 0
var x, y, button : int
loop
    Mouse.Where (x, y, button)
    if button not= 0 then
        x1 += sign (x - x1)
    end if
    angle := arctand ((y - y1) / (x - x1 + 1e-6))
    if angle < 0 then
        angle += 180
    end if
    x2 := x1 + cosd (angle) * 40
    y2 := y1 + sind (angle) * 40
    Draw.Line (round (x1), round (y1), round (x2), round (y2), 7)
    View.Update
    delay (5)
    cls
end loop

Get the angle and then use sine and cosine. In your case, you already have the rotated images, so you don't even have to use sine and cosine! Just draw the image that's at that angle.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: