Trigonometry and mouse

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
scienceman91
Newbie
Posts: 1
Joined: Fri Dec 04, 2009 6:53 pm

Trigonometry and mouse

Post by scienceman91 »

the code below is supposed to make the triangle aim at the mouse, the mouse is marked as a red cross

the triangle can be moved around with the arrow keys

but for some reason the more down and left you move it the more off the aiming is until eventually it doesn't work at all

can anyone help me with this please i cant seem to figure this out

Code: Select all

DIM SHARED degtorad AS DOUBLE
DECLARE SUB mouse (Funk)
COMMON SHARED sx AS INTEGER
COMMON SHARED sy AS INTEGER
TYPE playership
theta AS DOUBLE
x AS INTEGER
y AS INTEGER
xt AS INTEGER
yt AS INTEGER

END TYPE
COMMON SHARED ssx AS INTEGER
COMMON SHARED ssy AS INTEGER
common shared k as string
DIM SHARED p AS playership

DECLARE SUB drawship (p AS playership)
DECLARE SUB megaline (x AS INTEGER, y AS INTEGER, theta AS DOUBLE, r AS DOUBLE, col AS INTEGER)
DEF FNptrig
IF p.x - h > 0 THEN
IF p.y - v > 0 THEN
FNptrig = (ATN(ABS(p.y - v) / ABS(p.y - h)) * 180 / 3.14159) + 180
ELSEIF p.y - v <> 0 THEN
FNptrig = 90 - (ATN(ABS(p.y - v) / ABS(p.y - h)) * 180 / 3.14159) + 90
END IF
ELSEIF p.x - h <0> 0 THEN
FNptrig = 90 - (ATN(ABS(p.y - v) / ABS(p.y - h)) * 180 / 3.14159) - 90
ELSEIF p.x - h <> 0 THEN
FNptrig = (ATN(ABS(p.y - v) / ABS(p.y - h)) * 180 / 3.14159)
END IF
END IF
END DEF
degtorad = 3.14159 / 180
SCREEN 9, 0, 0, 1

p.theta = 30
p.x = 300
p.y = 200
DO

k=inkey$
if k=chr$(0)+"H" then
p.y=p.y-4
if p.y<0 then

end if
end if
if k=chr$(0)+"P" then
p.y=p.y+4

end if
if k=chr$(0)+"M" then
p.x=p.x+4
end if
if k=chr$(0)+"K" then
p.x=p.x-4
end if

CALL mouse(3)
p.theta = FNptrig
CALL drawship(p)
print p.theta
PCOPY 0, 1
CLS
LOOP WHILE 1 = 1

SUB drawship (ship AS playership)
CALL megaline(ship.x, ship.y, (ship.theta + 90) * degtorad, 10, 15)
LINE (sx, sy)-((40 * COS(ship.theta * degtorad)) + ship.x, (40 * SIN(ship.theta * degtorad) + ship.y)), 15
CALL megaline(ship.x, ship.y, (ship.theta - 90) * degtorad, 10, 15)
LINE (sx, sy)-((40 * COS(ship.theta * degtorad)) + ship.x, (40 * SIN(ship.theta * degtorad) + ship.y)), 15


END SUB

SUB megaline (x AS INTEGER, y AS INTEGER, theta AS DOUBLE, r AS DOUBLE, col AS INTEGER) 'draws angular lines
sx = (r * COS(theta)) + x
sy = (r * SIN(theta)) + y
LINE (x, y)-(sx, sy), col 'math and lines
END SUB

SUB mouse (Funk)
 SHARED B, h, v
 POKE 100, 184: POKE 101, Funk: POKE 102, 0
 POKE 103, 205: POKE 104, 51: POKE 105, 137
 POKE 106, 30: POKE 107, 170: POKE 108, 10
 POKE 109, 137: POKE 110, 14: POKE 111, 187
 POKE 112, 11: POKE 113, 137: POKE 114, 22
 POKE 115, 204: POKE 116, 12: POKE 117, 203
 CALL Absolute(100)
 B = PEEK(&HAAA)
 h = PEEK(&HBBB) + PEEK(&HBBC) * 256
 v = PEEK(&HCCC) + PEEK(&HCCD) * 256
LINE (h + 12, v)-(h - 12, v), 4
LINE (h, v - 12)-(h, v + 12), 4
END SUB

User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

1) FORGET DEF Fn ANYTHING! Make it into a REAL function! It's easy, just name it and use the name without FN. DEF returns may need an interum variable to alter them. DEF FN cannot be defined twice nor should it.

2) The code posted has one too many END IF statements before the END DEF.

3) Division by ZERO error.

4) The PEEK-POKE Absolute mouse is terrible. Use a real mouse routine.
I already tried that and it is almost useless! H, V and B is all it can do.

5) Finally your calculations must be wrong somehow. I'm not interested in pointing at my mouse if it is already pointing at me. :wink:

Good luck on that. It works sometimes, but at certain points it gets "stuck".

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply