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

Username:   Password: 
 RegisterRegister   
 Trouble Shooting trig
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BenLi




PostPosted: Wed Apr 26, 2006 2:48 pm   Post subject: Trouble Shooting trig

im trying to make a program to shoot a projectile and calculate the angle in which it was fired at. I get a error message saying "quit #100", help PLZ
code:

setscreen ("graphics:800;600,offscreenonly")
const speed : int := 25
const startx := 100
const starty := 100
var shotx, shoty : real
var mx, my, mb : int
var deltax, deltay : real
var b : real
var ang: real

loop
    shotx := startx
    shoty := starty
    loop
        mousewhere (mx, my, mb)
        exit when mb = 1
    end loop

    deltay := my - starty
    deltax := mx - startx

    b:= Math.Distance (startx, starty, mx, my)
   
    ang:= arcsind (((deltax **2) + (b**2) - (deltay**2))/2*deltax*b)
   
    loop
shotx:=shotx+(deltax/((Math.Distance(startx,starty,mx,my))/ speed))
shoty:= shoty+(deltay/((Math.Distance (startx, starty, mx, my)) / speed))
       
drawfilloval (round (shotx), round (shoty), 10, 10, red)
        View.Update
        cls
      put "Angle is ", ang
        exit when shotx > maxx + 10 or shoty > maxy + 10 or shotx < -10 or shoty < -10
    end loop
end loop
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Apr 26, 2006 3:03 pm   Post subject: (No subject)

sounds like you don't know how to use arcsind. Arcsind takes the original sine value, and returns the angle in degrees. So, originally, to determine the y-value, we have sine(theta) * hypotenuse. So, to find theta, we use sine inverse (y/hypotenuse). So, replace the line where you used arcsind with this:

code:
    b:= Math.Distance (startx, starty, mx, my)
    if b = 0 then
        b := 0.0001
    end if
    ang:= arcsind (deltay / b)

The if construct is there incase the hypotenuse was 0, and you can't divide by 0.

Btw, you should slow down the speed of your projectiles, cuz they're very hard to see. Other than that, a great program. Are you planning to do anything with it?
HellblazerX




PostPosted: Wed Apr 26, 2006 3:09 pm   Post subject: (No subject)

Sry, I forgot this in my first post. Add this piece of code after the arcsind function:

code:
if mx < startx then
    ang = 180 - ang
end if

Because arcsind only works for half of the circle, from -90 to 90, this part checks if we're working with the other half, and if so, changes the angle to the correct one
BenLi




PostPosted: Wed Apr 26, 2006 3:09 pm   Post subject: (No subject)

thanks, i asked my comp sci teacher for the syntax for cos inversion, and he just said arcsind... maybe i should've asked how to use it

and yes, im using it for a gunbound like game, i haven't put in gravity yet though, any idea how to do that?
Tony




PostPosted: Wed Apr 26, 2006 3:12 pm   Post subject: (No subject)

for gravity, just accelerate everything downwards by a constant during each tick of your game loop.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
BenLi




PostPosted: Wed Apr 26, 2006 3:15 pm   Post subject: (No subject)

But you would increase the grav value for every time it loops right?

i tried taht, didn't look good
Cervantes




PostPosted: Wed Apr 26, 2006 5:17 pm   Post subject: (No subject)

No. Gravity is constant. (Assuming you don't care about the negligable amount by which the force of gravitation attraction decreases as you move up a couple of metres)
Clayton




PostPosted: Wed Apr 26, 2006 7:01 pm   Post subject: (No subject)

for gravity you could do something like this
code:

const GRAV : real := 0.01
const RADIUS : int := 10
var x, y : real
var yChange : real := 1
var count : int := 0
x := maxx div 2
y := 0
View.Set ("graphics,offscreenonly")
loop
    exit when hasch
    loop
        cls
        Draw.FillOval (x, y, RADIUS, RADIUS, black)
        View.Update
        y += yChange
        if count not= 1 then
            yChange -= GRAV
            exit when y = 0
        else
            yChange += GRAV
            exit when y = 2
        end if
    end loop
    yChange := 1
    yChange := -yChange
    if count not= 2 then
        count += 1
    end if
    if count = 2 then
        count := 0
    end if
end loop

something like that will create a more realistic jump with gravity (im not 100% sure that that will run perfectly, dont quote me on that one) experiment with it and see what you can come up with
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: