"topdown turret"

miketv

  • Posts: 42
I'm trying to set up a mortar-launcher-type thing, & I'm wondering: how can I get my bullet to spawn at the end of my barrel?

Here's the setup:
I have a PLAYER (my turret) with an origin point set at Left-Center. When I hit the UP key, it rotates the barrel 5degrees clockwise, & when I hit the DOWN key it rotates the barrel 5degrees counterClockwise....so the end of the barrel is swinging up & down on the right side of the turret, basically.
When I hit the Right key, it creates & launches an ACTOR (my bullet) from the center of the turret....but that's not what I need. How do I offset the bullet so it comes from the end of the barrel & goes in the direction the barrel is pointing?

I've seen tutorials on how to do this with a mouse (like the Balloons example), but I can't find any info on how to do it when the barrel's angle is set by the keyboard. Any help is appreciated.

« Last Edit: January 10, 2014, 10:48:28 pm by miketv »

miketv

  • Posts: 42
I've got part of the problem solved.
I got the angle & direction of the barrel to dictate the angle & direction of the bullet by using
point LastCreatedActor towards DirectionOfSelf degrees and
push LastCreatedActor sharply towards DirectionOfSelf degrees at 160 force

 but I still can't get the bullet to spawn at the end of the barrel. Any help? Seems like I could use the
create Actor at (x:__ y:__) block, but what should I use to fill in the X & Y blanks for a rotating turret?

« Last Edit: January 11, 2014, 09:28:30 am by miketv »

colburt187

  • *
  • Posts: 2416
I have similar turrets in my game, unfortunately I don't have any useful advice for you. My bullet is created at the x center and y center of my cannon and then follows the barrel up and out the end, but at some angles you can see the bullet traveling just to the side of the barrel.

I would be interested to hear if anyone has a good solution for this.

mormord

  • Posts: 94
Little help from the trigonometry should help you.

bulletCX = cannonCX + cannonLength * cos( cannonAngle )
bulletCY = cannonCY + cannonLength * sin( cannonAngle )

CX = center X, CY = center Y
Please check that cannonAngle is in the right form (I mean you probably need radian inside the cos/sin function and you have degrees, but I'm not sure).

EDIT:
It is simplier to spawn the bullet in the center behind the cannon, just make sure the bullet won't collide with it.

« Last Edit: January 11, 2014, 04:01:29 pm by mormord »

Clunger

  • Posts: 11
Stencyl defaults to degrees.  Make sure you use the "__ as radians" block.

« Last Edit: January 11, 2014, 06:14:42 pm by Clunger »

miketv

  • Posts: 42
thanks for the advice. It's not working out quite like I expected, though. Maybe I'm using the radians block wrong?

Also, should cannonLength & cannonAngle be attributes or game attributes?
This 'attribute' stuff is very confusing....I don't get how the blue tags are supposed to know my cannon's length & angle...

mormord

  • Posts: 94
- cannonLength is the length of your cannon, depends on the actor you're using. Probably a constant, I would use an Normal (not game) Attribute. You can set it in the Actor's behaviors section.
- cannonAngle is the direction of your cannon. You are looking for [[direction of self] as radians] block I guess.
- this expression gives you the center of the bullet, while you need the top-left corner to create it. Subtract half of the width from the x position and half of the height from the y position.

There should be a FAQ for the attribute thing, because you're not the only one confused about it. If not, start a new question to it so anyone can find it who is interested (I wouldn't search something like that under the name "topdown turret" :) ) - and some guys who have better explaining skills than me, will probably answer you.

Hectate

  • *
  • Posts: 4643
An attribute is a way for the computer to hold data - such as a number value - for future use. It also helps you by making it easier to understand what is happening. For example, which of the two is more understandable for you?

set X Speed to (12 + 4 - 1) for (Actor ID 331)

or

set X Speed to ((BaseSpeed + PowerUp) - DeBuff) for (Player Actor)

Even if "cannonLength" is always exactly 16 pixels, it's far easier to read that you're using the length of the turret in the code and not something else that happens to also be "16". Plus, if you ever give the player a different tank with a longer turret, you don't have to go in and modify everything - just attach the same behavior to the new tank and set it's cannonLength to a different value. The code, if properly designed as we are showing you, will adapt to the new number without any problems.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

miketv

  • Posts: 42
thanks again for the input. I understand the concept of attributes, but the mechanics are where I'm having a problem. I'll start a new thread to that effect.


miketv

  • Posts: 42
if anyone wants an interactive example of the problem I'm having....
http://www.stencyl.com/game/play/23674

mormord

  • Posts: 94
I checked your example. Something definitly goes wrong. My guess is would be the angle storing wrong information. If you could upload your entire behav, I could tell you more (probably :) )

miketv

  • Posts: 42
I've got 3 behaviors:  cannonFire, cannonRotate, & one I downloaded from StencylForge called Face Direction of Motion

Not sure if I'm doing it right...I attached .PNGs. The Face Direction Of Motion behavior is broken into 2 pics, for some reason...it wouldn't let me copy the If and the Otherwise If blocks in the same pic.

If it helps:
cannonFire & cannonRotate are keyboard events I've attached to the cannon.
FDOM is an always event I attached to the shell.

Let me know if I'm not posting the info correctly

colburt187

  • *
  • Posts: 2416
Something going on in those last 2 blocks, your using x speed of self blocks in places I would expect to see x of self. That block is trying to work out an angle so don't think you want to put the speed numbers in there.

Unless your doing something more advanced than my understanding.

miketv

  • Posts: 42
Something going on in those last 2 blocks,

Those are the un-modified ones I got from Stencyl's standard behavior collection...it's the motion behavior called Face Direction Of Motion. It probably looks weird because it's converting the speed into an angle (I think that's what's going on, at least...)

colburt187

  • *
  • Posts: 2416
Ah I didn't relize it worked like that, I've used that behavior aswell. Well this is all a bit advanced for me, good luck with it.