Page 1 of 1

bullet past mouse help

PostPosted: Mon Nov 06, 2006 2:19 am
by Oman
Hi can someone explain to me in a simple way how to make your "player" actor create a bullet that goes toward the mouse, then goes past it and goes forever

can you please tell me in lamans terms, im new thx :D

PostPosted: Mon Nov 06, 2006 7:08 am
by sonicfire
I would do it this way:

First, create a new actor, call it "crosshair" !
Use a little image like this:
Image
Go to Actor Control and choose Events: Add: Create Actor: Add Action: Follow Mouse to make it stick to your mouse.

Now create a new actor, the player. I used a simple pre-rotated tank (sorry for crappy graphics):
Image
Download BMP tank image strip here: http://www.prodyon-virtual-gear.com/tutorial/mytank.bmp


It contains 36 Images, one image all 10 degrees. Remember: It has to face to east on start and must be rotated counter-clockwise(!)
Now create again a new actor, call it "player" or something. Load up the tank-animation.

Now, we need some (simple) script.
Choose the player-actor, select Events: Add: Draw Actor: Script Editor and type in the following:
Code: Select all
double mouseangle = direction(xscreen,yscreen, xmouse, ymouse);
animpos = (mouseangle/360.0)*(nframes);


This little code makes sure the Tank will always rotate and face the mouse (e.g. a crosshair).

Some explanations: double mouseangle declares a new variable called "mouseangle" of type double precision (32 bit).

The function "direction" returns the direction between points (x1, y1) and (x2, y2) (in degrees, 0 to 360, from positive x axis, counterclockwise, in degrees). (This function returns double precision values).

x/yscreen and xy/mouse return xy coordinates of screen and mouse position. (AFAIK, x/yscreen return the "on screen" position of an actor).
then we finally use some maths to calculate the image to be shown at that very moment. (nframes returns the number of frames the actor has (animation images)).

Okay back to work!
If you would start the game now, the tank should always face the mouse / the crosshair.

Ok, now lets add the bullet:
Create a new actor, call it "bullet" or whatever you like.
Image

Download BMP bullet image strip here: http://www.prodyon-virtual-gear.com/tutorial/bullet.bmp

(I´ve made it red here that you can see it better in the game).

Place this bullet also somewhere on your "scene".
Again go into Actor Control. First, choose "Create at startup: No" (upper right corner). Now go into Events: Add: Draw Actor: Script Editor:
Code: Select all
animpos = (angle/360.0)*(nframes);

This will ensure that it will always face the direction it is traveling.

Next thing: Events: Add: Create Actor: Script Editor:
Code: Select all
angle = direction(x, y, crosshair.x, crosshair.y);
directional_velocity = 8;

Here we set the bullets angle so that i will face to the mouse(crosshair). Then we apply velocity to it so that it will fly in that direction.
All this is done ONCE. And only when it will be created.

Final step -> Go into Actor Control of your "player". Choose Events: Add: Key Down: (Use e.g. Space Key) Select Repeat: Disable (!). Then choose "Create Actor" as the action from the list.
Choose Actor: bullet and leave the rest as it is.

Done! Now you should be able to shoot bullets in the direction of the mouse (as much bullets as you want).

If i have made any mistakes here, hopefully someone can correct me please :D

Oh and by the way: You should also check if the bullets are "out of view" and then delete them to save resources / CPU. I leave that to you :wink:

PostPosted: Mon Nov 06, 2006 8:03 am
by Diana Kennedy
Hey, that's cool. I never worked with that kind of thging, I will go ahead and try that out, just to learn that. 8)

PostPosted: Mon Nov 06, 2006 1:00 pm
by Oman
Thx that was great and easy to understand. Ill give it a try.Oh by the way I didnt need the player actor or the bullet to turn but I know how to sort out that code. This is great the other forums didnt explain it correctly
GOOD JOB. :D :D :D :D

PostPosted: Mon Nov 06, 2006 11:03 pm
by Oman
k i did that but when i press space the bullet is created but does not go towards my mouse. the bullet goes northwest on my screen can you help me wat did i do wrong :?

PostPosted: Tue Nov 07, 2006 8:58 am
by sonicfire
@Oman: Mhh, can you send me the *.ged-file so that i can have a look? (But i´m still on trial version if that matters :))

PostPosted: Tue Nov 07, 2006 1:12 pm
by Oman
could u tell me how i am only on the trial version to :?

PostPosted: Tue Nov 07, 2006 2:30 pm
by sonicfire
Mhh you can perhaps just save the GED file to disk and then send it to me? That should be allowed? If you want you can PM me!

PostPosted: Tue Nov 07, 2006 10:46 pm
by Oman
so how exactly do i pm u

im a nube i no :lol:

PostPosted: Sat Nov 18, 2006 2:38 am
by Oman
ok i got it thx guys :D :D :D

PostPosted: Mon Aug 13, 2007 3:45 pm
by J Maker
does anyone know how to make one actor turn so it will "still be facing" my player actor?