A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Depth and shooting

  1. #1
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784

    Depth and shooting

    Alright, I have a little bit of a problem. I am creating an average shooter game where things pop up behind things and you shoot them. Everything is going fine, however, I am having troubles figuring out how I can work the shooting. The problem is that, two objects could have the same x and y positions, but different depths. If I shot at the first one, I'd want the xscale of my projectile to be a lot larger than if I shot at the 2nd one. Also, if I shot at the first one, I'd want my projectile to move to my cross hairs fairly quickly, whereas if I shot at the distant projectile, my shot would take a little bit longer to reach the crosshairs. My only solution thus far is to check when the mouse is clicked to see what is currently at that x and y position and determine if it is close up or distant. I don't really like the solution and I can see a lot of problems with it. Any other ideas?
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Have you considered possibly opening a menu above the object(s) if two objects occupy the same position? This would enable you to select which target to fire at.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    need diagram please
    lather yourself up with soap - soap arcade

  4. #4
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    ok, I'm not too great at this type of thing, but here's a go at it.

    [edit] not sure why I can't get the images to show up, but here a link to the first one
    http://imageupload.com/~imageupl/sho...8330_temp2.gif


    This is an object that is flying across the screen.



    [edit] here is a link to the second one
    http://imageupload.com/~imageupl/sho...8329_temp1.gif

    This is an object that will popup from behind a roof.

    Both of the objects have the same x and y property and close to the same distance the bullet needs to travel to reach it. The problem is that when I shoot at the distant object, my bullet should have a lot smaller scale and take longer to reach that object versus the up closer and larger object.
    Last edited by TheLostGuru; 08-28-2007 at 10:24 AM.
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  5. #5
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    give every sprite or movieClip additional z- values that determine how far they are away - or..
    determine that value based on their _xscale/_yscale property but make sure that all movieClips have the same scale at the beginning (a house thus bigger as a car or character, ect.)
    Based on the depth values you can extend the timing and the buttet scale factors

  6. #6
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    In your second picture, you click on a distant object that is moving. If the user clicks ON it, do they hit it? Or do you expect the user to lead the object?

    If clicking ON it means they hit it, then how do you plan on having the shot visually hit that moving object? The shot can't move to where the click was, because the object won't be there anymore unless clicking on it stops it, which would be weird.

  7. #7
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    Render, I'm afraid it's not that easy, either that or I don't understand. And Alluvian, you are right. The shot is not immediate, so you will have to lead it a little bit. I've done up a little animation to hopefully explain it a little more.
    http://www.swfup.com/file/23433
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  8. #8
    Member
    Join Date
    May 2007
    Location
    North Carolina
    Posts
    95
    okay ... you are using scaling to resize your enemies ... you are also using scaling to resize your bullet ... just check it x, y and the scale of the object are equal ... if they are then you have a collision.

  9. #9
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    thanks, I understand how to check for a collision. I can handle that part. I still don't know how to handle shooting at the distance, or shooting up close.
    "If I have seen further it is by standing on the shoulders of giants." Isaac Newton
    ------------------------------------------------------------------------------

  10. #10
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    What several are saying you do the hitTest like normal, but then ALSO check the depth of each object and only run your 'hit' code if the depths also match.

    Quit is saying that since you are using _xscale and _yscale to visually represent the 'depth' of an object you don't even need a depth variable. You just have to make sure you draw everything to scale if you are going to take that shortcut. The door to the house at scale 100 would have to be just taller than the people at scale 100.

    If you decide to use depth anyway, then you just have to set the depth of each object and your bullet's onEnterFrame has to increment it's depth as it shrinks off into the distance.


    Your problem with how to move the bullet moves is that you are defining something that is pretty much impossible in 3d space. your gun is at the bottom of your screen at a fixed 3d position. Your crosshair however is not a POINT, it is really a LINE. From the eye of your player through the crosshair and on into infinity. there are any number of bullet paths you can draw from your gun point to the LINE created by your crosshair (into and out of the screen).

    What you are thinking is that you want the crosshair to work like a laserpointer and then the gun will shoot at the POINT that is the intersection of the line (between your eye, the crosshair), and the first object in the movie that this line hits.

    The problem is this behavior will cause some weird effects when aiming at nearby objects. If you click your crosshair on the very edge pixel of an object nearby, the gun will have to shoot at a very different 3d angle than if you click just a pixel or two to the side and miss that nearby object, instead now drawing a line maybe to some building hundreds of yards in the distance.

    I think most FPS games assume the crosshair is aiming at a near infinite depth object. But you can then possibly 'miss' a nearby object that you clicked on that is not moving. This becomes more apparent the further the gun is from your line of sight and the closer the object is. It is quite apparent in mech games where the guns are often way off on the sides and you are sometimes trying to shoot at a nearby foot trooper.

  11. #11
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    i´d say you need a distance/scale ratio variable so that you can recalculate the actual distance.

    But honestly like others I was irritated at the beginnning after all bullets fly faster you could even track with your eyes,- so all you´d need to know is if you hit the area and what scale the decal has.

  12. #12
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    If you go with the 'firing at a near infinite pont along the line of your crosshair' the math would actually be pretty easy I think.

    Basiclly the bullet will rapidly approach the point you clicked, but never quite get there (in the 2d plane of the screen, yea! infinity!).

    So, your bullet movement code would look kinda like this:

    PHP Code:
    var bulletspeed .5 // this is the multiplier for how much closer to your click spot the bullet will get each frame.  It has to be less than 1.  The larger, the faster the bullet converges on the clicked point.
    var bulletdepthdelta 1  //this is all relative to your fps setting, units, etc...  it is just how much deeper the bullet travels per frame.  JUST used for collision testing.

    bullet1.clickx _xmouse
    bullet1
    .clicky _ymouse
    bullet1
    .depth 0
    bullet1
    .maxrange 1000 //just a guess, this is tied to your depth values and depth delta.  at this depth, the bullet kills itself.  Lets you have different guns with different ranges too
    bullet1._x = (wherever your gun is)
    bullet1._y = (wherever your gun is)
    bullet1.onEnterFrame = function() {
       
    this._x -= (this._x this.clickx) * _root.bulletspeed
       this
    ._y -= (this._y this.clicky) * _root.bulletspeed
       
    // dont forget your scaling code to shrink the bullet.
       
    this.depth += _root.bulletdepthdelta 
       
    if (this.depth >= this.maxrange) {
          
    this.removeMovieClip
          
    //remove bullet from bulletarray (see below)
       
    }

    With bulletspeed at 0.5, this will move the bullet half the distance towards where you clicked with each frame. Smaller numbers will move it slower towards the convergence point, larger numbers will move it faster. a value of 0 will make it never move. A value of 1 will put it at the click point on frame 1.


    If your hittesting is on the enemies, then it would be something like this:
    //define a bulletarray value in your main somewhere and every time you shoot add a bullet to it, and when the bullets die, remove them from the bullet array.
    //make sure your baddie has a logical depth value

    PHP Code:
    //in the enemy onEnterFrame event:
    for (var i in _root.bulletarray){
      var 
    mybullet _root.bulletarray[i]
      if (
    this.hitTest(mybullet)){ //the movies overlap, but this is NOT a hit yet.
        
    if(this.depth == mybullet.depth) { //NOW we have a hit
          //do your melodramatic death code.
        
    }
      }

    Last edited by Alluvian; 08-28-2007 at 04:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center