Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Need help implementing shooting


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 klrr   Members   -  Reputation: 127

Like
0Likes
Like

Posted 07 July 2014 - 09:21 AM

I want to make a top-down shooter but I am unsure how to write the shooting. I know how to get the angle but I don't know how to calculate how and what I should apply to the X and Y position to the bullet that spawns each frame, can anyone explain how you do this>



Sponsor:

#2 klrr   Members   -  Reputation: 127

Like
2Likes
Like

Posted 07 July 2014 - 09:27 AM

Nevermind, I found out:

 

"Don't use angles: Compute the vector from the shooter to the mouse and normalize it (i.e. divide it by its length). Then advance the position of the bullet by adding that vector multiplied by the bullet's speed." - Álvaro



#3 klrr   Members   -  Reputation: 127

Like
-1Likes
Like

Posted 07 July 2014 - 09:51 AM

Tried the thing in the quote and it doesn't work.



#4 Buckeye   Crossbones+   -  Reputation: 5698

Like
3Likes
Like

Posted 07 July 2014 - 10:07 AM

Tried the thing in the quote and it doesn't work.

 

Uh.. are you just making a statement, or is there some code you could post, maybe describing what "it" is? Alvaro's method is a common way to do that sort of thing, and the method works.


Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.


#5 HappyCoder   Members   -  Reputation: 2829

Like
0Likes
Like

Posted 07 July 2014 - 03:30 PM

Also, how does it not work. The way it is malfunctioning may be a clue was to what is wrong.



#6 Kryzon   Prime Members   -  Reputation: 3243

Like
5Likes
Like

Posted 07 July 2014 - 05:36 PM

It's not that you shouldn't use angles. It's just another way of getting the same result: a set of components for you to translate the bullets each frame.
You can calculate this with an angle or with a vector.
 
// Angle.

Local deltaAngle:Float = Atan2( target.y - shooter.y, target.x - shooter.x )

Local velocityX:Float = Cos( deltaAngle ) * BULLET_SPEED
Local velocityY:Float = Sin( deltaAngle ) * BULLET_SPEED

// Vector.

Local deltaX:Float = target.x - shooter.x
Local deltaY:Float = target.y - shooter.y

Local length:Float = Sqr( deltaX * deltaX + deltaY * deltaY )

velocityX = ( deltaX / length ) * BULLET_SPEED
velocityY = ( deltaY / length ) * BULLET_SPEED

These methods compute the same practical result, a velocity vector for the bullet. You only need to compute this when you create the bullet -- you preserve the same velocity vector while you move the bullet.
The difference between these methods is that the vector method is usually faster and more precise. It only uses one function call (the square root).

#7 klrr   Members   -  Reputation: 127

Like
0Likes
Like

Posted 10 July 2014 - 03:29 AM

It's really that simple? Thanks a lot for clarifying Kryzon!



#8 klrr   Members   -  Reputation: 127

Like
0Likes
Like

Posted 10 July 2014 - 04:42 AM

Works kind of now. So it shoots bullets but they are shot in strange direction (not where I click). Here is code, java libgdx.

 

package com.deadsimple.zombiedown;

import com.badlogic.gdx.math.Vector2;

public class Bullet extends Sprite {
    
    public Bullet(Vector2 start, Vector2 dest) {
        super(start.x, start.y, 50, 50, 100);
        float deltaX = dest.x - start.x;
        float deltaY = dest.y - start.y;
        
        float length = (float) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
        this.velocity.x = (deltaX / length) * this.SPEED;
        this.velocity.y = (deltaY / length) * this.SPEED;
    }
    
    public void update(float dt) {
        this.move();
        chackAlive();
    }

    private void chackAlive() {
        if (this.position.x > 480 || this.position.x < -50) this.alive = false;
        if (this.position.y > 800 || this.position.y < -50) this.alive = false;
        
    }
    
}



#9 unbird   Crossbones+   -  Reputation: 5590

Like
0Likes
Like

Posted 10 July 2014 - 05:49 AM

Do you account for the size of the bullet ? Meaning, is it drawn so its center of mass (visually) is actually at this.position ?



#10 Thinias   Members   -  Reputation: 139

Like
0Likes
Like

Posted 11 July 2014 - 01:58 AM

From a directional perspective, the implementation presented here looks correct to me.  I would check and see if the "dest" you passed in is actually where you thought it was.



#11 Kryzon   Prime Members   -  Reputation: 3243

Like
0Likes
Like

Posted 11 July 2014 - 02:06 AM

Please also post the move() method, it may be where the problem lies.

#12 Orymus3   Crossbones+   -  Reputation: 10121

Like
0Likes
Like

Posted 11 July 2014 - 09:18 AM

If using angles, read about atan2()
This is a usual math lib method that is a life saver :)
That's how I handled my bullets.

#13 Orymus3   Crossbones+   -  Reputation: 10121

Like
0Likes
Like

Posted 11 July 2014 - 04:24 PM

also should call your function checkAlive (typo) and should not contain magic numbers (worst case make them a global constant for now so you keep your eyea on them)

#14 klrr   Members   -  Reputation: 127

Like
0Likes
Like

Posted 13 July 2014 - 06:54 AM

Thanks for all help, the problem was that I used the mouse position in the screen resolution, not as game world coordinates.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS