Skip to main content

News

Topic: Scripting a bullet with collision on the map engine (Read 3591 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Scripting a bullet with collision on the map engine
so im making my RPG and i ran into a snag

things on the map have their own set of coordinates for collision etc, however, when they are drawn onscreen (like a flying bullet) a new set of screen coordinates has to be whipped up. should my map objects themselves keep their onscreen coordinates, or should they have methods to derive that information? or should the methods that refresh the ui and draw things to the screen calc those positions from the objects map coord's as they draw?

i have been leaning towards the map objects have methods that hand that info over, but i feel like there might be a cleaner way to do it.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: maintaining the map and drawing the screen
Reply #1
Map Entities have GetPersonX(name) and GetPersonY(name) as well as SetPersonX(name) and SetPersonY(name);
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: maintaining the map and drawing the screen
Reply #2
I don't think that's what he's asking. I think he's looking for ScreenToMapX(layer, x), ScreenToMapY(layer, y), MapToScreenX(layer, x), and MapToScreenY(layer, y). Not really sure what he's trying to say though.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: maintaining the map and drawing the screen
Reply #3
Oh, I see, yeah Alpha123, that would be what he'll need, if he goes his route. He's drawing bullets on the render script (screen coordinates) which will need converting to the map coords for collision detection. But there's a simpler way.

What he'll want to do (to use Sphere's collision system) is to create the bullet as an entity rather than on screen, and use the Set/GetPersonX/Y() functions I showed him. SetPersonX/Y() ignores collision which is ideal when moving the bullet through terrain. In fact you can turn off terrain collision by setting SetPersonTileObstructions("name", false); Then when you call IsPersonObstructed("name", x, y), it'll only trigger when the bullet comes in contact with another person rather than a tile.

So that means to not use a render script and screen coordinates, and forego them for just using the map coordinates and setting a bullet with the SetPersonX/Y() functions as it moves.

I'd create an off-map representation of the bullet:
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);
}

Bullet.prototype.update = function() {
   SetPersonX(this.name, GetPersonX(this.name) + this.xVelocity * this.speed);
   SetPersonY(this.name, GetPersonY(this.name) + this.yVelocity * this.speed);
}


Then when your hero creates a bullet you can do something like:
Code: (javascript) [Select]

var bullets = [];
var bullet_num = 0;

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);
}

function UpdateBullets()
{
    for (var i = 0; i < bullets.length; ++i) bullets[i].update();
}


And then to attach it to the map engine:
Code: (javascript) [Select]

function Update()
{
  // your update script stuff
  UpdateBullets();
}

function game() {
    // your game code

    SetUpdateScript("Update()");
    MapEngine("map.rmp", 60);
}


Then you can also add collision detection to the update portion of the bullet:
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;
    }
}

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


Then if you want to get the name of the entity the bullet collided with (to do damage) you can use the GetObstructingPerson()
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, x, y);
        if (name != "") {
            // here we have an obstructing entity..
        }
    }
}


edit: fixed code tags
  • Last Edit: July 07, 2013, 02:45:54 pm 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: maintaining the map and drawing the screen
Reply #4
yes radnen that exact system i already had in place! i think i was over complicating the problem by not using MapToScreen(), simply because i tend to shy away from calls to the map engine.

  • DaVince
  • [*][*][*][*][*]
  • Administrator
  • Used Sphere for, like, half my life
Re: maintaining the map and drawing the screen
Reply #5

yes radnen that exact system i already had in place! i think i was over complicating the problem by not using MapToScreen(), simply because i tend to shy away from calls to the map engine.

Hey, if you're using the map engine, you don't need to shy away from its functions, right? :)

Re: maintaining the map and drawing the screen
Reply #6
well i have a little hud for like health bars and stuff, and chat bubbles, and for mouse clicks, so its like i have to get a full set of persons coord's almost every frame, so i just saved it all in my own list, and im putting the combat stats and everything in there, but its like, for the mouse clicks and chat bubbles, im practically forced to have some sort of medium between the map person and the mouse object... right? or am i over complicating things? or does it even matter if i over complicated it?

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: maintaining the map and drawing the screen
Reply #7

well i have a little hud for like health bars and stuff, and chat bubbles, and for mouse clicks, so its like i have to get a full set of persons coord's almost every frame, so i just saved it all in my own list, and im putting the combat stats and everything in there, but its like, for the mouse clicks and chat bubbles, im practically forced to have some sort of medium between the map person and the mouse object... right? or am i over complicating things? or does it even matter if i over complicated it?


I feel the exact same way sometimes. It's okay to overcomplicate things (as long as you mean it in a good way). If nothing slows the game down then don't worry. You can always go back and change things if they don't work.
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