Computer Science Canada

need help with pic.rotate

Author:  CodeMonkey2000 [ Thu Nov 02, 2006 7:05 pm ]
Post subject:  need help with pic.rotate

im making a game with a spider-bot-tank-thingy, and i need help with rotating the turret. i'm using pic.rotate, but when i rotate it, the whole turret doesnt show up. this isnt the full source, but just the part im having trouble with.

code:

setscreen ("offscreenonly")
var turret1, turret : int

drawfillbox (2, 4, 6, 18, 15)
drawfilloval (4, 4, 4, 4, 15)

turret := Pic.New (0, 0, 8, 18)
Pic.Draw (turret, 96, 95, picCopy)
loop
    for x : 1 .. 360
        turret1 := Pic.Rotate (turret, x, 4, 4)
        cls
        Pic.Draw (turret1, 150, 150, picCopy)
        View.Update
    end for
end loop


Author:  bruized [ Thu Nov 02, 2006 7:20 pm ]
Post subject: 

I think that all you need to do is create a large margin on the bottom and left side so that when it does rotate the picture it doesn't cut off your actual turret.

Author:  CodeMonkey2000 [ Thu Nov 02, 2006 7:36 pm ]
Post subject: 

how would you do that? Pic.Rotate (picID, angle, x, y : int) : int
x and y are where it should rotate, and angle is well, the angle.

Author:  bruized [ Thu Nov 02, 2006 7:41 pm ]
Post subject: 

Your turret is imported from a picture right? All you have to do is edit the picture in Adobe Photoshop or something and add some space on the bottom and on the left.

Author:  CodeMonkey2000 [ Thu Nov 02, 2006 7:46 pm ]
Post subject: 

the picture is not imported, at the begining it is drawn through turing's modules, and then it takes a "screenshot" and uses that as a reference later.

Author:  bruized [ Thu Nov 02, 2006 7:48 pm ]
Post subject: 

Sorry, I'm not thinking clearly and not really paying attention. If I could I'd delete the post before, but i can't. If you were importing the picture then it would work but you're not... Confused how could I be so stupid (*shakes head*)

Author:  richcash [ Thu Nov 02, 2006 8:15 pm ]
Post subject: 

If you're picture is not imported, then why don't you rotate everything you draw in your turret using sine and cosine?

Author:  bruized [ Thu Nov 02, 2006 8:25 pm ]
Post subject: 

Either that, or you could simply draw the picture farther away from the bottom left and when you Pic.New simply leave more space on the bottom left hand corner. That way there is adequate space to allow for the clipping not to be seen (it only clips part of the background of the picture not the actual turret.

Like so...

code:
setscreen ("offscreenonly")
var turret1, turret : int

drawfillbox (38, 40, 42, 54, 15)
drawfilloval (40, 40, 4, 4, 15)

turret := Pic.New (0, 0, 60, 60)
Pic.Draw (turret, 96, 95, picCopy)
loop
    for x : 1 .. 360
        turret1 := Pic.Rotate (turret, x, 40, 40)
        cls
        Pic.Draw (turret1, 150, 150, picCopy)
        View.Update
    end for
end loop


That was done quick so it may not be what you want it, but the main idea is I drew the picture farther away from the bottom left which left more room, yada yada yada. You get the point Wink

Author:  CodeMonkey2000 [ Thu Nov 02, 2006 10:34 pm ]
Post subject: 

wow, thnx

Author:  Dan [ Thu Nov 02, 2006 11:18 pm ]
Post subject: 

Ack, each time that loop is run it is making a new pic and never freeing it. Affter some time it will use up all the space turing alows for pics and will crash. You need to add a Pic.Free at the end of the forlop.

Author:  DemonZ [ Fri Nov 03, 2006 2:09 am ]
Post subject: 

no no thats not it, if u want the pic to not cut off while rotating, the when you do Pic.Rotate (pic, angle, x, y) set the parameters of x and y as -1, this will rotate the picture in the specified margin without cutting it off, and if u want it to rotate according to the center, than just draw ur pic but when u do the x and y subtract the x and y values form the Pic.Height and Pic.Width of ur pic, here is some sample source-pseudo code for u to experiment and understand with:
code:
PSEUDO:
% rotates tank turret
for i: 1 .. 35
Tankturret (i) := Pic.Rotate (P1_Tank (0), i * 10, -1, -1)
end for
% draws tank turret
    Pic.Draw (Tankturret (Tank_angle), round (x) - Pic.Width (Tankturret (Tank_angle)) div 2, round (y) - Pic.Height (Tankturret (Tank_angle)) div 2, picMerge)
% Draws ur tank

This way your x and y are centered in the middle and ur boundaries are set to -1 so that all of the picture is rotated within the margins
If u dont understand what I mean just ask.

Author:  bruized [ Fri Nov 03, 2006 3:53 pm ]
Post subject: 

I definitely don't understand what you mean. I just don't really get it. It doesn't click.

I also don't understand what Hacker Dan said. Was he talking about my code? When you pic.rotate it actually creates a totally new picture? or what...

I'm kinda new and so I don't know a lot about Turing.

Author:  CodeMonkey2000 [ Fri Nov 03, 2006 7:01 pm ]
Post subject: 

i get what hackerdan was saying. if you let the privious program run, it will eventually slwo dwon ur comp, then crash.
code:

setscreen ("offscreenonly")
var turret1, turret, angle : int := 1
var rotate : boolean := false

drawfillbox (38, 40, 42, 54, 15)
drawfilloval (40, 40, 4, 4, 15)

turret := Pic.New (0, 0, 60, 60)
Pic.Draw (turret, 96, 95, picCopy)

loop
    angle += 1
    if angle = 360 then
        angle := 1
    end if
    cls
    turret1 := Pic.Rotate (turret, angle, 40, 40)
    Pic.Draw (turret1, 150, 150, picCopy)
    Pic.Free (turret1)
    rotate := false
    locate (1, 1)

    View.Update
end loop


not sure what demonZ mnet....

Author:  DemonZ [ Sat Nov 04, 2006 2:33 pm ]
Post subject: 

ok first lets start with the basics: if u want to have a game where the player is going to rotate then you will need to familiarize yourself with these commands:
COMMANDS
Pic.Rotate
arrays
round
sind
cosd
Pic.Height
Pic.Width
for loops
These are just some of the most important commands youll need to know, first lets start with declaring some variables
code:

var controls : array char of boolean % This is for input.keydown
var x, y, dx, dy : real % These must be real in order for the rotate to fully work
var tankangle : int
var picture : array 0 .. 35 % The picture that you are using to make ur tank rotate must be an array so that it can store all the rotated pictures from 0 to 360 degrees

Now lets assign the values to these variables
code:

x := % put where ever you want ur tank to start out at
y := % put where ever you want ur tank to start out at
dx := 0
dy := 0
picture (0) := Pic.FileNew ("Name of picture") % The subscript beside picture is 0 to let u know that 0 degree is the original degree of the pic

Now we will use a for loop to actually generate the rotated versions of the pic
for i : 1 .. 35
picture (i) := Pic.Rotate (picture (0), i * 10, -1, -1)
end for

Now here are the controls for the tank to rotate
code:

loop
Input.KeyDown (controls)
if controls (KEY_UP_ARROW) then
x := x - dx
y:= y + dy
dx := sind (tankangle * 10) % this will rotate the tank by 1 degree times 10 ex 1 := 10 degrees 24 := 240 degrees
dy := cosd (tankangle * 10) % the same thing here
elsif controls (KEY_DOWN_ARROW) then
x := x + dx
y := y - dy
dx := sind (tankangle * 10)
dy := cosd (tankangle * 10)
elsif controls (KEY_LEFT_ARROW) then
tankangle := (tankangle + 1) mod 35 % This will make sure that when the picture is fully rotated, it will reset back to zero according to the mod
elsif controls (KEY_RIGHT_ARROW) then
tankangle := (tankangle - 1) mod 35
end if
Pic.Draw (picture (tankangle), x + (Pic.Width (picture (tankangle) div 2), y + (Pic.Height (picture (tankangle) div 2), picMerge) % actually draw the pic after the if statement

Now We Will put all of this together and we will get the picture to rotate

Author:  DemonZ [ Sat Nov 04, 2006 2:47 pm ]
Post subject: 

and if u didnt understand that, then here is an example of Pic.Rotate used in action with a box drawn in turing
code:

var controls : array char of boolean
var x, y, dx, dy : real
var picture : array 0 .. 35 of int
var angle : int

drawfillbox (0, 0, 50, 100, blue)
picture (0) := Pic.New (0, 0, 50, 100)
cls

x := 100
y := 100
dx := 0
dy := 0
angle := 0

for i : 1 .. 35
    picture (i) := Pic.Rotate (picture (0), i * 10, -1, -1)
end for

setscreen ("graphics:max,max,offscreenonly")

loop
    Input.KeyDown (controls)
    if controls (KEY_UP_ARROW) then
        x := x - dx
        y := y + dy
        dx := sind (angle * 10)
        dy := cosd (angle * 10)
    elsif controls (KEY_DOWN_ARROW) then
        x := x + dx
        y := y - dy
        dx := sind (angle * 10)
        dy := cosd (angle * 10)
    elsif controls (KEY_LEFT_ARROW) then
        angle := (angle + 1) mod 35
    elsif controls (KEY_RIGHT_ARROW) then
        angle := (angle - 1) mod 35
    end if
    Pic.Draw (picture (angle), round (x) - Pic.Width (picture (angle)) div 2, round (y) - Pic.Height (picture (angle)) div 2, picMerge)
    delay (20)
    View.Update
    cls
end loop

sorry the last post had a couple mistakes in it that i forgot to correct this is how u do it

Author:  Dan [ Sat Nov 04, 2006 2:56 pm ]
Post subject: 

It is an intresting idea to make all the anagles and save them as pics befor you start the game. It whould make the game run slightly faster but use up more memory. It is the calsic memory effsheny vs time effshenscy trade off.

The thing to rember is that turing has a limt on how many pics it can have in memeory. So if your game is going to need alot of pics in memeory at one time it maybe best to use the dynamic creation of pics method so you only use up one pic place at a time. However since you are only using 35 pics in that area in most cases it probly whould be better to put them in the area like you are.

Either way just rember to free the pics affter you are done with them other wise you will run in to issues.

Author:  DemonZ [ Sat Nov 04, 2006 3:38 pm ]
Post subject: 

The way I suggested works, but if you prefer using less memory than try a different approach, I used this technique when I made the controls for my tank game and it ran good, only problem you might run itno your game is if your using projectiles and how to shoot them according to the angle and the collsion checking, in that case I can help you with it.


: