News:

Function Finder  Find all the functions within source code files

Main Menu

2D overhead mouse natvigation

Started by mar4000, November 01, 2006, 03:54:48 AM

Previous topic - Next topic

mar4000

Recently, I have seen a few games in which the mouse become the targeter and the character of the game walks/moves to wards it and always faces it. How can we do this with playbasic or more to the point, how do we calculate the angle of the mouse relative to a point on the screen?

Example:http://www.miniclip.com/games/final-fortress/en/
600x400
800x600
1024x768

kevin

#1
PlayBASIC Code: [Select]
Setfps 60

gunx#=100
Guny#=getscreenheight()-100

Do
cls 0

mx=mousex()
my=mousey()

; calc the angle from the gun to the mouse pointer
angle#=getangle2D(gunx#,guny#,mx,my)



;curve the angle tomake it lag a bit behind the mouse.
Currentangle#=curveangle(angle#,currentangle#,20)

; project a point from the guns origin
x2#=cosnewvalue(gunx#,currentangle#,100)
y2#=sinnewvalue(guny#,currentangle#,100)

; draw a line as the gun
line gunx#,guny#,x2#,y2#
Sync
loop






mar4000

Thank you!
I need to start using the v1.5 function and commands...
Im too use to a older version! Used asin and acos and pythagoras therom to calculate stuff, and now theres a command!?
Oh well. I like the new version.
600x400
800x600
1024x768

kevin


   The snippet should work in really old version of PBs, like lower than PB1.089.  Which is really old now (almost 2 years old) come to think of it.

    Might I ask what revision your currently using ?