forums

TNT Basic Forums > Programming Q&A
shooting at mouse pointer
< Last Thread     Next Thread >
Author
Thread        Post A Reply

12-10-2003 20:07

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

im making a game similar to robotron but i wanted to use the mouse to control the direction of the shots instead of more buttons, however i cannot seem to do it, is there an easy way of doing this as im sure my game is not the first to try this. im reluctant to use hardware mode as its to slooooow and my shots are round any way so they dont need rotating.
ive tried all sorts but the best i can come up with is getting a sprite to rotate around my character like a satelite when i move the mouse horizontally.


thanks
charlie

12-11-2003 01:59

Posted by:
JAS

Click Here to Email JAS   Find more posts by JAS

I posted a similar question a while back: http://tntbasic.com/community/forums/showthread.php?threadid=657

Hope that helps.

12-11-2003 19:04

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

yeah, i read that but im having trouble getting bullets to fire where the pointer actually is.

ill have another go but any more suggestions would help greatly

thanks
charlie

12-12-2003 04:54

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

The one in the link should work...maybe you used integers instead of floats to calculate the point.

12-14-2003 16:31

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

nope, still no joy.

i have the most trouble finding out where the mouse is in relation to the main character, its really starting to bug me.

thanks
charlie

12-14-2003 19:25

Posted by:
JAS

Click Here to Email JAS   Find more posts by JAS

I've been teaching myself some trig the past couple of days in order to get what you're talking about and I figured it out 2 days ago. i was gonna upload this example of SHOOTING, AIMING, ROTATING using Sin and Cos, but I'll just post the code here. If there is something that you don't understand - due to my uncommented code (which I'll comment soon, promise) just ask.

Here it goes - it was a good successful experiment - gotta love those!

hide mouse
Graphics Mode hardware 640,480
Open Canvas 1,640,480
set framerate 30
load images 128

int ingame=0,quit=false
int degree=45,radius=50,mx,my,x,y,mouse=0,planet=1
int tymer,shooter=3,shtrx=400,shtry=200,shotx[12],shoty[12],shotimage[12],rad[12],maxshots=12,shoton[12],shotsprite=11,shotangle[12],aimangle
int deg

draw picture 130

for ingame=0 to ingame=1
ingame=0

mx=mouse x
my=mouse y

degree=degree+10

x=radius*cos(degree)+mx
y=radius*sin(degree)+my

if space then radius=radius+3
if not space and radius>50 then radius=radius-3

sprite planet,x,y,1
sprite mouse,mx,my,0

'********************START SHOOTING EXAMPLE*********************
tymer=tymer+1

if tymer>15
tymer=0
int openshot=0
for openshot=0 to maxshots-1
if shoton[openshot]=false
shoton[openshot]=true
shotangle[openshot]=calculate angle(shtrx,shtry to mx,my)
shotangle[openshot]=shotangle[openshot]+270
shotx[openshot]=rad[openshot]*cos(shotangle[openshot])+shtrx
shoty[openshot]=rad[openshot]*sin(shotangle[openshot])+shtry
shotimage[openshot]=1
set image bank openshot+shotsprite,128
sprite openshot+shotsprite,shotx[openshot],shoty[openshot],shotimage[openshot]
break
end if
next openshot
end if

int moveshot=0
for moveshot=0 to maxshots-1
if shoton[moveshot]
rad[moveshot]=rad[moveshot]+3
shotx[moveshot]=rad[moveshot]*cos(shotangle[moveshot])+shtrx
shoty[moveshot]=rad[moveshot]*sin(shotangle[moveshot])+shtry
if shotx[moveshot]>640 or shotx[moveshot]<-32 or shoty[moveshot]>480 or shoty[moveshot]<-32
shoton[moveshot]=false
rad[moveshot]=0
sprite off moveshot+shotsprite
else
sprite moveshot+shotsprite,shotx[moveshot],shoty[moveshot],shotimage[moveshot]
end if
end if
next moveshot

aimangle=calculate angle(shtrx,shtry to mx,my)
aimangle=aimangle+270
set sprite rotation shooter,aimangle

sprite shooter,shtrx,shtry,0

if mouse button
ingame=1
end if
draw frame
next

12-15-2003 20:21

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

wow!
thats exactly what i was after!
is calculate angle actually a built in function in tnt?
that would have made life easier from the start
i guess i should read the helpfile more often when i get stuck

either way, thanks my game will be up to speed in no time!

cheers
charlie

12-16-2003 15:59

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

just one thing about the example, if you set the firing sprite so that it can move, then the bullets also move in the same direction

thanks again
charlie

12-16-2003 16:38

Posted by:
charlie

Location:
norfolk, england

Click Here to Email charlie   Find more posts by charlie

i hope you dont think im being too cheeky but ive changed your code a bit so that the above doesnt happen

Graphics Mode hardware 640,480
Open Canvas 1,640,480
set framerate 30
load images 128

int ingame=0,quit=false
int degree=45,radius=50,mx,my,x,y,mouse=0,planet=1
int tymer,shooter=3,shtrx=400,shtry=200,shotx[120],shoty[120],shotimage[120],rad[120],maxshots=120,shoton[120],shotsprite=11,shotangle[120],aimangle
int deg,oldshtrx[120],oldshtry[120]

'draw picture 130

for ingame=0 to ingame=1
ingame=0

mx=mouse x
my=mouse y

degree=degree+10

x=radius*cos(degree)+mx
y=radius*sin(degree)+my

if space then radius=radius+3
if not space and radius>50 then radius=radius-3

sprite planet,x,y,1
sprite mouse,mx,my,0

'********************START SHOOTING EXAMPLE*********************



tymer=tymer+1

if tymer>3
tymer=0
int openshot=0
for openshot=0 to maxshots-1

if shoton[openshot]=false
oldshtrx[openshot]=shtrx
oldshtry[openshot]=shtry
shoton[openshot]=true
shotangle[openshot]=calculate angle(oldshtrx[openshot],oldshtry[openshot] to mx,my)
shotangle[openshot]=shotangle[openshot]+270
shotx[openshot]=rad[openshot]*cos(shotangle[openshot])+oldshtrx[openshot]
shoty[openshot]=rad[openshot]*sin(shotangle[openshot])+oldshtry[openshot]
shotimage[openshot]=1
set image bank openshot+shotsprite,128
sprite openshot+shotsprite,shotx[openshot],shoty[openshot],shotimage[openshot]
break
end if
next openshot
end if

int moveshot=0
for moveshot=0 to maxshots-1
if shoton[moveshot]
rad[moveshot]=rad[moveshot]+10
shotx[moveshot]=rad[moveshot]*cos(shotangle[moveshot])+oldshtrx[moveshot]
shoty[moveshot]=rad[moveshot]*sin(shotangle[moveshot])+oldshtry[moveshot]
if shotx[moveshot]>640 or shotx[moveshot]<-32 or shoty[moveshot]>480 or shoty[moveshot]<-32
shoton[moveshot]=false
rad[moveshot]=0
sprite off moveshot+shotsprite
else
sprite moveshot+shotsprite,shotx[moveshot],shoty[moveshot],shotimage[moveshot]
end if
end if
next moveshot

aimangle=calculate angle(shtrx,shtry to mx,my)
aimangle=aimangle+270
set sprite rotation shooter,aimangle
sprite shooter,shtrx,shtry,0

if up then shtry=shtry-5
if down then shtry=shtry+5
if left then shtrx=shtrx-5
if right then shtrx=shtrx+5


if mouse button
ingame=1
end if
draw frame
next



ive added two new variables oldshtrx and oldshtry which are both arrays
and simple movement for the shooting sprite

oldshtrx and oldshtry take only the value of shtrx and shtry once instead of every frame which stops them moving when you move the shooting sprite



12-16-2003 20:15

Posted by:
JAS

Click Here to Email JAS   Find more posts by JAS

I didn't need it in that example, but it's cool that you pointed it out. Shortly after writing that I used it in my game and added the oldshtrx array in it.

Cheeky - not at all. We're all here to learn - thanks for pointing it out!

Good luck with your game!

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.