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

Username:   Password: 
 RegisterRegister   
 Firing bullets - angles - help?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Remm




PostPosted: Fri May 19, 2006 9:20 am   Post subject: Firing bullets - angles - help?

OK, if anyone had seen my earlier program in'A question on shooting...', lets just say its alot better now.
However, Wink
I seem to be having problems with multiple angles, like for every move you get a different angle of shot. Having troubles with this.

What ive been trying to use is a rise over run (aka (y2-y1) / (x2 - x1) ) to get the angle of fire and then put it into a y=mx+b thing to get the trajectory of the bullet. However, I dont know what to do with the b.
example:
(This so at any angle x and y, at any angle, can shoot the bullet along that angles line -- will work great for my game ^_^ ... if it works... )
code:


var x, y : int
var m : real
x := 370
y := 55
Draw.ThickLine (380, 35, x, y, 7, brightred)
m := (y - 35) / (x - 380)
put m
for a : 1 .. 100
    Draw.FillOval (x, y, 2, 2, blue)
    delay (100)
    Draw.FillOval (x, y, 2, 2, white)
    x := x + 5
% y  =           m     x  +  b
    y := round (m * x) + 800
end for



What should i really put for the 800 (b)?
and is this a good way to be doing this, or is there a easier way? I looked at the one in the flexable arrays example and got all confused. Confused
Thanks,
Remm
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Fri May 19, 2006 10:19 am   Post subject: (No subject)

If you're going to work with angles, you're going to have to learn trig. It is much easier. See, with your method, you find m, which is the slope not angle. There's a difference. Then, you must substitute a set of points into your equation to determine b, which is the y-intercept. Then you plug into an x-value in to get a y-value, can drawing a line to it. With trig, you can just use the cosine and sine functions to get the x and y values.
Remm




PostPosted: Fri May 19, 2006 2:22 pm   Post subject: (No subject)

Confused
uhh, you just confused the crap outta me. Im doing sine / trig n all that stuff in school atm, yet i still don get what ur trying to tell me. Agh, im gunna mingle around in my text book until i find out wth to do, will post what i come up with within a day. Wish me luck. Very Happy

Remm
Remm




PostPosted: Fri May 19, 2006 2:48 pm   Post subject: (No subject)

sorry for double posting but...
btw, if anyone could help me with getting the formula going in turning, it would be much appriciated. I now see that im not getting anywhere in slope, so i have to do it the trig way. I see the sind and other commands for it, i jus need to know how to get the angle and how to make THAT shoot. Not seeing how to get the x/y values out of it Sad ty again, i'll b trying to find out myself for now.
TheOneTrueGod




PostPosted: Fri May 19, 2006 4:16 pm   Post subject: (No subject)

Angles are not the same thing as slope.

The equation (x2-x1)/(y2-y1) will not get you the angle between two points, only the slope.

You have two options for getting the angle between two points. Use the Math.Angle function included in turing, or create your own Angle function.

In order to use it in turing, you have to know a few things about trig first.
I'll assume you know the basics, such as sin(0) = 0, sin(90) = 1, cos(0) = 1, and cos(90) = 0, and most importantly, WHY.

Allright, with that basic knowledge, you can break up a line segment into two components: its x component, and its y component.

--/ | <-- Y component
/t_|
^
X component

In order to determine the x component, we just take the hypoteneuse (sp?) and multiply it by the cosine of theta (Represented by t in the diagram above.)

The y component is similarily determined by multiplying the hypoteneuse by the sine of theta.

Syntax in turing is as follows:

Radians
sin(angle : real)
cos(angle : real)
tan(angle : real)

Degrees
sind(angle : real)
cosd(angle : real)
tand(angle : real)

Using this information, lets apply it to a very minor program (That I'm not going to bother testing, so if theres any syntax errors, fix them on your own Razz)

code:

const moverate := 3
var mx,my,but : int
var x,y : real := 100
loop
   mousewhere(mx,my,but)
   drawfilloval(round(x),round(y),5,5,black)
   x += cosd(Math.Angle(x,y,mx,my)) * moverate
   y += sind(Math.Angle(x,y,mx,my)) * moverate
   Time.DelaySinceLast(30)
   cls
end loop


And theres the basics of it Very Happy

If you want to get into more advanced stuff, there are plenty of good programs in the source code section (Be sure to read the comments before you study the code though, so you can learn which programs aren't good examples Razz)

Anyways, good luck
Remm




PostPosted: Fri May 19, 2006 4:29 pm   Post subject: (No subject)

Shocked
Ack - my turing doesnt have Math.Angle... ?!!?
So, any ideas on that 'create your own function'? lol -.-
and i KNOW angles arnt same thing as slope, just very poor wording.

Quote:

I'll assume you know the basics, such as sin(0) = 0, sin(90) = 1, cos(0) = 1, and cos(90) = 0, and most importantly, WHY.


uh... yeah... our teacher has yet to tell us why. We jus click the button on calculator an it does stuff - no need to think!
I think i'll ask em on monday.
TheOneTrueGod




PostPosted: Fri May 19, 2006 4:44 pm   Post subject: (No subject)

Allright, Number one, here, you can use my function:
code:

function Angle (x1, y1, x2, y2 : real) : real
    %Created by TheOneTrueGod.
    var theta : real
    if x1 = x2 then
        if y2 > y1 then
            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end Angle


Just leave the "created by TheOneTrueGod" line in there, or give me credit somehow and its all good Very Happy

Anyways, if you plan on doing some programming over the weekend, there are many great tutorials on the web, and there may even be one or two in the [Turing Tutorials] section. Just look around Very Happy

(And be sure not to use the function unless you know what it does, or else you're not going to understand what your code is doing... not a good thing Razz)
Cervantes




PostPosted: Fri May 19, 2006 5:59 pm   Post subject: (No subject)

Remm wrote:

Ack - my turing doesnt have Math.Angle... ?!!?

I don't think any of them do, unless v4.1 does.

Here's a tutorial by zylum that outlines using trig to move a spaceship. You'll probably find it useful.
Sponsor
Sponsor
Sponsor
sponsor
Remm




PostPosted: Sat May 20, 2006 9:25 am   Post subject: (No subject)

Thanks guys, i'll try the thing u gave me god, ran it alone and it didint spam errors so, s'all good. Time to put it to use Smile
Remm




PostPosted: Sat May 20, 2006 6:30 pm   Post subject: (No subject)

Alrighty, thanks to you all, ive gotten it to shoot at least a LITTLE bit properly. I know it still has massive issues, but its a start. I found out what functions are and how to use em per your instructions before using it, since before i was totaly unhinged trying to call up the function. Now, yet another question Confused
How do i shoot more than one bullet at a time? Try turning the SHOOT procedure into a process - thats what i see as a dead end with everything i try. can anyone point me in the right direction as to where i can find it out / any idea for getting the angle thing to work. once i GOT the angle, i was prty blank as what to do next, so i got that strange thing up n running. Dont i have to sine it or somthing?

Heres the code so ye kno what im dealin with:

code:

%============================
var font1 : int
var mx, my, mb : int
var win1 : int
var cannon :
    record
        x : int
        y : int
        firex : int
        firey : int
        firemove : int
        angle : real
    end record

%==============================
cannon.x := 380
cannon.y := 73
font1 := Font.New ("Comic Sans MS:30")

win1 := Window.Open ("title:Final Gunner;graphics:800;600;position:center;top")
Draw.ThickLine (380, 35, cannon.x, cannon.y, 7, brightred)
Draw.FillOval (380, 33, 20, 23, black)
Draw.Oval (380, 33, 20, 23, brightred)
Draw.FillBox (360, 3, 400, 33, black)
Draw.FillBox (370, 3, 390, 13, brown)
Draw.Line (380, 3, 380, 13, black)
Draw.Dot (382, 8, black)
Draw.Dot (378, 8, black)
Draw.Dot (382, 9, black)
Draw.Dot (378, 9, black)
Draw.Dot (382, 7, black)
Draw.Dot (378, 7, black)
Draw.FillBox (365, 23, 395, 18, brightblue)
Draw.Line (400, 34, 360, 34, brightred)
Draw.FillOval (380, 35, 5, 5, gray)
Draw.Line (385, 40, 375, 30, black)
Draw.ThickLine (0, 0, maxx, 0, 6, green)

View.Set ("offscreenonly")

%==========================================================================

procedure tower  %look! now its a procedure! ^_^
    var towermove : array char of boolean
    loop
        Draw.ThickLine (380, 35, cannon.x, cannon.y, 7, white)
        if (cannon.x > 380) then
            Input.KeyDown (towermove)
            if towermove (KEY_RIGHT_ARROW) then
                cannon.x := cannon.x + 1
                cannon.y := cannon.y - 1

            elsif towermove (KEY_LEFT_ARROW) then
                cannon.x := cannon.x - 1
                cannon.y := cannon.y + 1
            end if
        elsif (cannon.x < 380) then
            Input.KeyDown (towermove)
            if towermove (KEY_RIGHT_ARROW) then
                cannon.x := cannon.x + 1
                cannon.y := cannon.y + 1

            elsif towermove (KEY_LEFT_ARROW) then
                cannon.x := cannon.x - 1
                cannon.y := cannon.y - 1
            end if
        elsif (cannon.x = 380) then
            Input.KeyDown (towermove)
            if towermove (KEY_RIGHT_ARROW) then
                cannon.x := cannon.x + 1
                cannon.y := cannon.y - 1

            elsif towermove (KEY_LEFT_ARROW) then
                cannon.x := cannon.x - 1
                cannon.y := cannon.y - 1
            end if
        end if
        if (cannon.x = 420) then
            cannon.x := cannon.x - 1
            cannon.y := cannon.y + 1
        end if
        if (cannon.x = 340) then
            cannon.x := cannon.x + 1
            cannon.y := cannon.y + 1
        end if
        Draw.ThickLine (380, 35, cannon.x, cannon.y, 7, brightred)
        Draw.FillOval (380, 33, 20, 23, black)
        Draw.Oval (380, 33, 20, 23, brightred)
        Draw.FillBox (360, 3, 400, 33, black)
        Draw.FillBox (370, 3, 390, 13, brown)
        Draw.Line (380, 3, 380, 13, black)
        Draw.Dot (382, 8, black)
        Draw.Dot (378, 8, black)
        Draw.Dot (382, 9, black)
        Draw.Dot (378, 9, black)
        Draw.Dot (382, 7, black)
        Draw.Dot (378, 7, black)
        Draw.FillBox (365, 23, 395, 18, brightblue)
        Draw.Line (400, 34, 360, 34, brightred)
        Draw.FillOval (380, 35, 5, 5, gray)
        Draw.Line (385, 40, 375, 30, black)
        View.Update
        delay (30)
    end loop
end tower

%===========================================================================

%================================================ Created by TheOneTrueGod
function angle (x1, y1, x2, y2 : real) : real
    var theta : real
    if x1 = x2 then
        if y2 > y1 then

            theta := 90
        else
            theta := 270
        end if
    else
        theta := arctand ((y1 - y2) / (x1 - x2))
        if x2 > x1 then
            if y2 > y1 then
            else
                theta := 360 + theta
            end if
        else
            theta := 180 + theta
        end if
    end if
    result theta
end angle
%===================================================Created by TheOneTrueGod


procedure SHOOT
    cannon.firemove := 0
    cannon.firex := cannon.x
    cannon.firey := cannon.y
    cannon.angle := angle (380, 35, cannon.firex, cannon.firey)
    cannon.angle := cannon.angle / 100
    var changex, changey, a, b : real
    changex := 10 * (.9 - cannon.angle)
    changey := 10
    a := 0
    b := 0
    for x : 1 .. 80
        a := a + changex
        b := b + changey
        Draw.FillOval (cannon.firex + round (a), cannon.firey + round (b), 4, 4, black)
        delay (25)
        Draw.FillOval (cannon.firex + round (a), cannon.firey + round (b), 4, 4, white)
       
    end for
end SHOOT


%===========================================================================

process fire
    var fire : array char of boolean
    loop
        Input.KeyDown (fire)
        if fire (KEY_TAB) then
            SHOOT
        end if
    end loop
end fire

%============================================================================

fork fire
tower



as you can see i've been steering away processes as much as possible. They are evil.
Cervantes




PostPosted: Sat May 20, 2006 6:44 pm   Post subject: (No subject)

Remm wrote:

How do i shoot more than one bullet at a time? Try turning the SHOOT procedure into a process - thats what i see as a dead end with everything i try.

I'm glad to see you're going to avoid processes. To get this done, you'll need to learn Flexible Arrays. There's an example of just this in that tutorial.

You've got quite a steep learning curve right now, it would seem. Good stuff!
Remm




PostPosted: Sat May 20, 2006 7:09 pm   Post subject: (No subject)

yeah but... every time i look at flexable arrays i get very confused and angry at them. Turings help explination of it is horrid to say the least. I kinda know how to do arrays themselves, i guess i'll try to fortify my understanding of arrays then move on to flexables. Thanks again. some day the bullets will acually be shooting at somthing ^_^
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  [ 12 Posts ]
Jump to:   


Style:  
Search: