Skip to main content

News

Topic: Shooting with Map Wrapping: Is It Really Possible? (Read 2177 times) previous topic - next topic

Shooting with Map Wrapping: Is It Really Possible?
I edited Radnens shooting script so that the boundaries are limited by a variable bullet_distance  8)

Code: (javascript) [Select]

var bullets = [];
var bullet_num = 0; // how many bullets where shot
var bullet_distance = 64;
var bd_north,bd_east,bd_south,bd_west;

//Bullet is an Object with arguments
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; //general speed of bullet

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

//creates and positions bullet according to player direction
function CreateBullet(player)
{
    var x = 0, y = 0;
    switch(Direction) {
        case "north": y = -1;bd_north = py - bullet_distance;break; //north -y
        case "south": y = 1;bd_south = py + bullet_distance;break; //south +y
        case "west": x = -1;bd_west = px - bullet_distance;break; //west -x
        case "east": x = 1;bd_east = px + bullet_distance;break;   //east x
    }
bullet_num++;
//creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
    bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y));
}

Bullet.prototype.update = function() { //whenever the game updates this method is run for Bullet object prototype
    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 ( y <= bd_north ||y >= bd_south ||x <= bd_west ||x >= bd_east) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "") {
            // obstruction = some entity by name
        }
    }
}

function UpdateBullets()
{
    for (var i = 0; i < bullets.length; ++i) {
        bullets[i].update();
        if (bullets[i].dead) { DestroyPerson(bullets[i].name); bullets.splice(i, 1); i--; }
    }
}


It works perfect and there are no problems UNTILL the player walks in the blue zone. The shooting system then glitches, the bullet creates itself and then imedtiatly destroys itself untill the player moves out of the blue zone and shots in another direction.

Here is the update script, I moved the UpdateBullets(); up but the glitch is still there.

Code: (javascript) [Select]

var moving = false;
var px = GetPersonX(Player);
var py = GetPersonY(Player);
var Direction;

function Update()
{
//Update Global Variables
Direction = GetPersonDirection(Player);
moving = false;
px = GetPersonX(Player); //update and get player x position
py = GetPersonY(Player); //update and get player y position
//Movement
IsPersonObstructed(Player,px,py)
if (IsKeyPressed(KEY_UP)) {
         QueuePersonCommand(Player, COMMAND_FACE_NORTH, true);
     QueuePersonCommand(Player,COMMAND_MOVE_NORTH,false);
     moving = true;           
}
else if (IsKeyPressed(KEY_DOWN )) { 
     QueuePersonCommand(Player, COMMAND_FACE_SOUTH, true);
     QueuePersonCommand(Player, COMMAND_MOVE_SOUTH, false);
     moving = true;          
}
else if (IsKeyPressed(KEY_LEFT)){   
     QueuePersonCommand(Player, COMMAND_FACE_WEST , true);
     QueuePersonCommand(Player, COMMAND_MOVE_WEST , false);
     moving = true;     
}
else if (IsKeyPressed(KEY_RIGHT)) {      
     QueuePersonCommand(Player, COMMAND_FACE_EAST , true);
      QueuePersonCommand(Player, COMMAND_MOVE_EAST , false);
      moving = true;
}
//Bullet
UpdateBullets();
if (!IsKeyPressed(KEY_SPACE)) key_hit = false;
if (IsKeyPressed(KEY_SPACE) && key_hit == false)
{
key_hit = true;
CreateBullet(Player);
}
}


Is it possible to have a shooting system work in a wrapping map? I had a feeling that some co-ordinates could get mixed up while the player wraps around the map. The glitch if left in would help the player know when they are wrapping over a map but I would rather this not be the case so the player gets absolutely lost. I'm making a survival horror game (mwahahaha). Thanks for your support once again.

Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #1
I've noticed something more specific about the glitch, when the players co-ordinates are specifically at x = 0 or y = 0 the bullet creates itself in an unusual location and then immediately destroys itself instead start_x or start_y beginning at the next tile ( 32 pixels away from the player ).The bullet does not move and immediately destroys itself. The glitch stays only until the player turns and shoots in the opposite direction. So if I shoot while I am wrapping around the map the glitch would occur if I happen to press shoot at x = 0 or y = 0 then it doesn't go away until I turn and shoot in the opposite direction while moving and shooting in the opposite direction works more immediately.
  • Last Edit: July 12, 2013, 05:33:19 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #2
Well, one problem is the fact that your distances are not stored per bullet. That is the key. Since bd_* are global variables as soon as you fire a second bullet you screw up the first one. To fix that, each bullet needs to have an origin stored. You in fact don't need to store each individual direction. You can use Pythagoras theorem to get the actual distance and compare that to 64 (bullet_distance) to see if the bullet has gone far enough.

The modifications are shown below; you only need to add 5 lines of code and modify one.
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; //general speed of bullet
    this.startX = start_x;
    this.startY = start_y;

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

Bullet.prototype.update = function() { //whenever the game updates this method is run for Bullet object prototype
    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);

    var dx = x - this.startX;
    var dy = y - this.startY;
    var dist = Math.sqrt(dx*dx + dy*dy); // pythagoras theorem a^2 + b^2 = c^2
   
    if (dist >= bullet_distance) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "") {
            // obstruction = some entity by name
        }
    }
}


Edit: also, get the players direction each time a bullet is created like this:
Code: (javascript) [Select]

//creates and positions bullet according to player direction
function CreateBullet(player)
{
    var x = 0, y = 0;
    switch(GetPersonDirection(player)) {
        case "north": y = -1; break;
        case "south": y = 1; break;
        case "west": x = -1; break;
        case "east": x = 1; break;
    }
    bullet_num++;
    //creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
    bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y));
}


I don't know if that solves your problem until you start testing. But this is the standard procedure used to introduce distances into games. The alternative is to create a timer and just let the bullet go until the timer runs out, but if you do that you won't know the max distance. It depends on the question really: do I want to not know the max distance but have it move for a certain time? Or do I want to know the max distance but not know when it'll die? Anyways, what I suggested is indeed a good start and understanding of using some algebraic and trigonometric features to make a game more interesting.

Ah, I remember first using Pythagoras theorem to fade out a waterfall sound over distance. That was a very enlightening experience and I thank DaVince still to this day for telling me about that. Once you "get it", so many doors open. Well, have fun! /old-timer rant
  • Last Edit: July 12, 2013, 05:34:09 am by Radnen
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 with Map Wrapping: Is It Really Possible?
Reply #3
 ;D awsome idea, I would have never thought about that. Let me first try using Pythagoras. Then the timer but I want to learn some algebraic and trigonometric features like you suggest and the doors will open to some really interesting gameplay  ;).

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #4
Did you take trigonometry in highschool? Granted it took a while before some concepts sunk in, but using math in games really helps - alot! I remember thinking one day: Why in the heck would I ever use 'sin' for? I've never been so wrong in my life! 0.0
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 with Map Wrapping: Is It Really Possible?
Reply #5
Same here, actually most of the time in high school I never really payed much attention in maths class  ::) mainly because to me it seemed too theoretical and repetitive, I would mostly blame the boring lessons. Maths itself is actually very practical and interesting.
I wish I had a more open minded teacher, actually when I was a bit younger I really loved Maths for its straight forward simplicity and solving problems always gives you that feeling of accomplishment and enlightenment. In high-school it was more about getting the grade, questions about use in video-games which where my biggest interest at that time too or anything "out of the syllabus" was flushed down with Cambridge pass-papers and repetitive lessons. Do numbers 1 to 10, their like the same thing over and over and over. I understand that this is just how the British Education system works ( tried and tested ) but I got really bored and thought to myself maths sucks. because of that I mostly forgot a lot out of disinterest and lack of use in more engaging situations ( or in other words It was not of my interest anymore because it was just about the grade not about learning and challenging yourself ).

I'm a quick learner though so it wont be a bother trying out some maths concepts and now I get to challenge myself so I'm sure these concepts will stick for life. My mind seems to reject anything over repetitive and useless/unpractical. I just have to relate what I have learned to game development or something that sparks my interest(^_^).

Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #6
 ;) It works absolutely perfect. My skills have leveled up 8)

Here is an attachment to elaborate on my excitement.
  • Last Edit: July 12, 2013, 09:21:28 am by Xenso

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #7
Oh my god, I am laughing my ass off here.

(Though I'm not worthy of that name. :P)

Re: Shooting with Map Wrapping: Is It Really Possible?
Reply #8
you are dude...you are  8)