Skip to main content

News

Topic: Shooting and Collision Detection (Read 6760 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Shooting and Collision Detection
How can I program a simple shooting system like in Arkista's Ring for the NES



The player presses a button, lets say space-bar and a bullet sprite is spawned in front of the player that moves forward at a fixed speed, it then destroys itself when it hits a wall or any other entity. I know how it would work but I'm not a programmer at least yet.
I would need a bullet sprite that creates itself from the location of the players co-ordinates and then moves in the direction the player is facing until it hits an object. A variable could store the players direction but I'm a bit shaky on how to do all this.
I'm reading the Wiki but I learn best when I just dive right in and use the knowledge I learn practically with some help from experienced programmers like Randnen and allot of you (^_^).

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Shooting and Collision Detection
Reply #1

The player presses a button, lets say space-bar and a bullet sprite is spawned in front of the player that moves forward at a fixed speed, it then destroys itself when it hits a wall or any other entity. I know how it would work but I'm not a programmer at least yet.
I would need a bullet sprite that creates itself from the location of the players co-ordinates and then moves in the direction the player is facing until it hits an object. A variable could store the players direction but I'm a bit shaky on how to do all this.
I'm reading the Wiki but I learn best when I just dive right in and use the knowledge I learn practically with some help from experienced programmers like Randnen and allot of you (^_^).

Well, you basically dissected the problem! That's a programmer's way of thinking. :)

Sorry for the wiki being so incomplete! We lost a lot of articles some time ago. But the function list should give you an idea of what each function does, anyway.

In this case, it might be useful to check out the following functions to help you: CreatePerson(), DestroyPerson(), SetPersonXYFloat(), IsPersonObstructed(), SetPersonScript(), GetPersonDirection(), QueuePersonCommand(), SetPersonSpeed(). Have the bullet act as a person and use these functions to control it in the way you mentioned, basically. You can also ask me why I'd suggest some of these functions if you can't figure it out. :)

Re: Shooting and Collision Detection
Reply #2
Hey thanks  ;)
And yes I would like to ask you why you chose SetPersonXYFloat(). Doesn't Float have something to do with numbers with decimals? I'm guessing this is more precise.
The rest make sense to me but I need to check out YOUR (Davince's) scripting tutorial to catch up with some basic coding. Sphere's still beautiful though and pregnant with possibilities  ;D.

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Shooting and Collision Detection
Reply #3
Quote
And yes I would like to ask you why you chose SetPersonXYFloat(). Doesn't Float have something to do with numbers with decimals? I'm guessing this is more precise.

It's not the floating point which is important there, but the fact that you can set the person's x and y position at the same time. There's no SetPersonXY() for some reason. But you can also use SetPersonX() and SetPersonY() if you want; it'll just be two functions then. :)

Quote
The rest make sense to me but I need to check out YOUR (Davince's) scripting tutorial to catch up with some basic coding. Sphere's still beautiful though and pregnant with possibilities  ;D.

Drat! Time to properly format some of the later chapters! :P

Re: Shooting and Collision Detection
Reply #4
I went through all of it today, it was fine but I skipped the object section for now. Its a lot though and thanks for the help again. I'll get something going maybe tomorrow or later. I'm wiped out right now (-_-) but it was very helpful. I forgot a lot of simple things (^_^).

  • N E O
  • [*][*][*][*][*]
  • Administrator
  • Senior Administrator
Re: Shooting and Collision Detection
Reply #5
Moved to Script Support.

Regarding existing shooter code to study, I'd recommend studying my Artyxx tech demo once I clean it up. It's a little advanced and attempts to handle collision both in and out of the default map engine.

Re: Shooting and Collision Detection
Reply #6
That would be great if you could share it (^_^)  but I will make a simple script.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #7
You know there is a good code example I made in the topic here: http://forums.spheredev.org/index.php/topic,55.0.html

That should get you on the right track, but it does require you know a bit about programming in JS (technical level = moderate).
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Shooting and Collision Detection
Reply #8
Code: [Select]

function CreateBullet(hero)
{
    var d = GetPersonDirection(hero);
    var x = 0, y = 0;
    switch(d) {
        case "north": y = -1; break;
        case "south": y = 1; break;
        case "west": y = -1; break;
        case "east": y = 1; break;
    }


I was studying your code and have 2 questions:
Why is it Y only, that is set-up, I thought with computer co-ordinates, If the top left of the computer screen has co-ordinates of X = 0 and Y = 0 then:

North would be a +Y number
East would be a +X number
South would be a -Y number
West would be a -X number

Also I though we would create the bullet in front of the player so it doesn't check if its been obstructed when its created over the player/hero.
If my player is 16 pixels all round, I'm guessing Sphere checks X and Y positions from the center of the sprite ( I hope so ). So the bullets XY co-ordinates would need to be + or - 8 ( half of 16 ) from the players XY co-ordinates. So the code for the case statements would be.

        case "north": y = +8; break;
        case "east": x = +8; break;
        case "south": y = -8; break;
        case "west": x = -8; break;
       
Or I might be misunderstanding something.

the second question is I noticed the arguments in the bullet.push:

Start_x = GetPersonX(hero) + x*16
Start_y = GetPersonY(hero) + y*16

how did you come to this? I'm a bit confused. 
  • Last Edit: July 07, 2013, 03:34:27 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #9
You are right the top-left is (0, 0) which is why I'm right in the direction vectors.

Y is up. Going up means you head towards 0, which is a negative number. ;)

North would be a -Y number
East would be a -X number
South would be a +Y number
West would be a +X number

This code:
Start_x = GetPersonX(hero) + x*16
Start_y = GetPersonY(hero) + y*16

Moves the bullet one square ahead of the player. Think about grabbing the players position and then adding to it the direction vector times the tile size (which is 16). You thought right when you said we need to create it in front of the player. This is how it's done. ;)
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Shooting and Collision Detection
Reply #10
 :) It makes sense.

Whats a direction vector, I'm not getting it on Wikipedia (http://en.wikipedia.org/wiki/Direction_vector) and need some freshening up on my maths  :o  ::).

I get the code after some dry testing and I think we need to use x for east and west:

Start_X = GetPersonX(hero) + x*16

example: hero is facing north at co-ordinates (hero_x,hero_y)

Start_X = hero_x + (0)*16 //case is north so y = -1 and x remains as 0
Start_X = hero_x + 0
Start_X = hero_x

therefore x is the same co-ordinates as our hero

Start_Y = hero_y + (-1)*16
Start_Y = hero_y - 16

example: hero is facing east, same generic co-ordinates.

Start_X = hero_x + (0) //case east, y = 1 and x = 0
Start_X = hero_x

but it never started at east 16 pixels. wont we need to need to change the case statement

Quote

North would be a -Y number
East would be a -X number
South would be a +Y number
West would be a +X number


case "east" : x = -1; break;
case "west" : x = 1; break;
//the rest north and south are the same as previous case code
  • Last Edit: July 07, 2013, 06:13:47 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #11
You are absolutely right. The y's on east and west should be changed to x's.

I just changed the original source, thank you. :)

There are so, so many vector pages on Wikipedia it's almost not worth searching. :/

A "direction vector", is well just my loose term for a vector in general (all vectors encode a direction). A vector is basically an integer that encodes a speed and direction. -1 is a speed of 1 pixel going down. If it were -2, it would be a speed of 2 pixels going down, and things would be placed twice as far away from the player character.

It was a neat way of showing how to encode a direction mathematically from the players direction. Of course your idea of literally adding +/- 8 or 16 pixels will do the same thing, but just not as adaptable code-wise.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Shooting and Collision Detection
Reply #12
Yeah your method is much neater, thanks I remember vectors. Wikipedia really confused me, I was getting worried for nothing.


Code: (javascript) [Select]
 if (IsPersonObstructedAt(x, y)) {this.dead = true;}



how does the computer know the co-ordinates of x,y


Code: (javascript) [Select]
function CreateBullet(hero)
{
    var d = GetPersonDirection(hero);
    var x = 0, y = 0;
    switch(d) {
        case "north": y = -1; break;
        case "south": y = 1; break;
        case "west": x = -1; break;
        case "east": x = 1; break;
    }

    bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(hero) + x*16, GetPersonY(hero) + y*16, x, y);
}



I thought x and y where local not global variables in function CreateBullet();


CODE EDITED
  • Last Edit: July 07, 2013, 10:34:40 am by Xenso

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Shooting and Collision Detection
Reply #13
Just a note: it's always better to put code in [code=javascript][/code] tags, even when you're citing code. Because JS code like [i] will disappear as it's interpreted as bbcode. :)

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting and Collision Detection
Reply #14
Oh wow, there are q few issues with it.

Bullet needs a speed member:
Code: (javascript) [Select]

function Bullet(name, start_x, start_y, vel_x, vel_y)
{
    this.name = name;
    this.xVelocity = vel_x;
    this.yVelocity = vel_y;
    this.speed = 1;

    CreatePerson(name, "bullet.rss", true); // name of this bullet;
    SetPersonX(name, start_x);
    SetPersonY(name, start_y);
    IgnoreTileObstructions(name);
}


And you are right about x and y, I wanted to have local variables:
Code: (javascript) [Select]

Bullet.prototype.update = function() {
    var x = GetPersonX(this.name) + this.xVelocity * this.speed;
    var y = GetPersonY(this.name) + this.yVelocity * this.speed;

    SetPersonX(this.name, x);
    SetPersonY(this.name, y);

    if (IsPersonObstructedAt(x, y)) {
        this.dead = true;
        var name = GetObstructingPerson(this.name, GetPersonX(this.name), GetPersonY(this.name));
        if (name != "") {
            // here we have an obstructing entity..
        }
    }
}


I'll update the source as well, yikes!
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here