Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Connecting scrolling map and collisions  (Read 9552 times)

0 Members and 2 Guests are viewing this topic.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« on: June 17, 2010, 02:54:12 pm »
Hello all :)

I made successful my scrolling map and collisions, but when I tried to connect it - sth was wrong.

http://wklej.to/v46L

I am wondering whats wrong, becouse without map scrolling, collisions were workin'.

Kind Regards
Fuv

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #1 on: June 18, 2010, 09:13:29 pm »
BTW, in the future it would help if you could prepackage all of your project/code you want to test because copying and pasting and building projects is a pain.

Maybe the problem is you need to App.SetView(App.GetDefaultView()); before App.Draw(Collisioner); ?

Or the offset needs to be added to the collsion object's position?

A simple test would be to print the position of the objects before and after scrolling to see if they are changing and by how much.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #2 on: June 19, 2010, 02:29:39 pm »
I found out where the problem is, but I don't know how to solve it.

I ve done as u had said and I wrote possition both player and collisioner. And I noticed that Collisioner is always on the possition (100,100) (I want it to be there), but my player also has always the same possition (512, 384).

I know that player isnt moving - just view, but I don't know how shouls I do to detect collision is that way. Could you give me any advice?

Kind Regards

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Connecting scrolling map and collisions
« Reply #3 on: June 19, 2010, 04:55:19 pm »
So move your player instead of moving your view.

If you want your view to follow the player, then after having moved your player, move your view to the player's position.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #4 on: June 20, 2010, 02:32:34 am »
You can also expand your collision class for views.

Something like...

if view exists and moves..

collision position+=view offset

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #5 on: June 20, 2010, 04:13:03 pm »
Thanks.

I made camera moving with player. I added:

Code: [Select]
Player.SetCenter(Player.GetSize() / 2.f);
Camera.SetCenter(Player.GetPosition());


And then moved my view.


It is almost working. I just need to know why I can't trigger:
Code: [Select]
App.SetView(App.GetDefaultView());

If I use this function collision is not handle and player is moving faster than camera does. On the other hand if I don't use it player is not dimensional (it is smaller than it should be) and collision location is moved a bit left and up.

All my code is here:
http://wklej.to/k602

And class I used for detect collisions here:
http://www.sfml-dev.org/wiki/en/sources/simple_collision_detection

Kind Regards
Fuv

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #6 on: June 24, 2010, 02:08:32 pm »
Is anybody able to help me?

Kind Regards
Fuv

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #7 on: June 25, 2010, 08:10:00 am »
Here I coded a working collision demo for you:

Code: [Select]

#include <SFML/Graphics.hpp>
#include "collision.h"
#include <iostream>
#include <string>

int main()
{
    sf::RenderWindow App(sf::VideoMode(1024,768),"Collision Demo");
    App.SetFramerateLimit(60.f);

sf::Image IMGBackground,IMGPlayer,IMGobstacle;
IMGBackground.LoadFromFile("bg.jpg");
IMGPlayer.LoadFromFile("player.png");
    IMGobstacle.LoadFromFile("obstacle.png");

sf::Sprite Player(IMGPlayer);
sf::Sprite Background(IMGBackground);
sf::Sprite Obstacle(IMGobstacle);

    Player.SetCenter(Player.GetSize()*.5f);
    Obstacle.SetCenter(Obstacle.GetSize()*.5f);

Background.Resize(2000,2000);
Background.Move(-500,-500);
Player.Move(512,384);
Obstacle.Move(100,100);

    sf::Vector2f Center(1000,1000);
    sf::Vector2f HalfSize(App.GetWidth()*.5f,App.GetHeight()*.5f);
    sf::View View(Center,HalfSize);

sf::Event evt;

    std::string lastKey="";
    float timer;
while(App.IsOpened())
{
while(App.GetEvent(evt))
{
if(evt.Type==sf::Event::Closed){App.Close();}
if(evt.Type==sf::Event::KeyPressed&&evt.Key.Code==sf::Key::Escape){App.Close();}
}

timer=App.GetFrameTime();

if(App.GetInput().IsKeyDown(sf::Key::Down)){if(Collision::BoundingBoxTest(Player,Obstacle)==false){Player.Move(0,100*timer);lastKey="D";}else if(lastKey=="D"){Player.Move(0,-100*timer);}else{Player.Move(0,100*timer);}}
if(App.GetInput().IsKeyDown(sf::Key::Up)){if(Collision::BoundingBoxTest(Player,Obstacle)==false){Player.Move(0,-100*timer);lastKey="U";}else if(lastKey=="U"){Player.Move(0,100*timer);}else{Player.Move(0,-100*timer);}}
if(App.GetInput().IsKeyDown(sf::Key::Right)){if(Collision::BoundingBoxTest(Player,Obstacle)==false){Player.Move(100*timer,0);lastKey="R";}else if(lastKey=="R"){Player.Move(-100*timer,0);}else{Player.Move(100*timer,0);}}
if(App.GetInput().IsKeyDown(sf::Key::Left)){if(Collision::BoundingBoxTest(Player,Obstacle)==false){Player.Move(-100*timer,0);lastKey="L";}else if(lastKey=="L"){Player.Move(100*timer,0);}else{Player.Move(-100*timer,0);}}

View.SetCenter(Player.GetPosition());

App.Clear();
App.SetView(View);
App.Draw(Background);
App.Draw(Obstacle);
App.Draw(Player);
App.Display();
}

return 0;
}

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #8 on: June 26, 2010, 02:31:08 pm »
Thanks really a lot. I am so grateful. :)

I have one more question. If I want to make collision with more object what is the fastest way to do that? Have I to write all conditions each time?

I tried that way:
Code: [Select]
if(App.GetInput().IsKeyDown(sf::Key::Down))
 {
 if((Collision::BoundingBoxTest(Player,Obstacle)==false) && (Collision::BoundingBoxTest(Player, Obst2) == false))
 {
 Player.Move(0,100*timer);
 lastKey="D";
 CFPS.Move(0, 100*timer);
 }
 else if(lastKey=="D")
 {
 Player.Move(0,-100*timer);
 CFPS.Move(0,-100*timer);
 }
 else
 {
 Player.Move(0,100*timer);
 CFPS.Move(0,100*timer);
 }
 }


Obst2 is my 2nd object that I want to collide with.

But it works not well. The collision place is moved left and up. I have image if you don't understand what I am talkin about:
http://img96.imageshack.us/img96/796/83280459.jpg

How to solve that?

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #9 on: June 27, 2010, 07:01:28 am »
I think the problem is you are either setting the center before scaling the sprite or not setting it at all.


For managing lots of collision you want to put moving objects in an array/vector and cull the ones that cannot reach your player or are offscreen.

For a static map you can create a grid or a smaller image with a mask color where every pixel represents 50x50 pixels. Then you move a scaled representation of the character over it and detect if the character is touching the masked color.

Another way you can do it is by drawing polygons over your map so you can do angles or slopes.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #10 on: June 28, 2010, 02:37:33 pm »
Thanks for advices. I was wondering about it much time :)


Backing to my moved collision. I changed something it code and not it looks better... but not perfectly. Collision area is a bit smaller than image. Moreover I also use animation from here:
http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated

And that complicates it more. So here I give part of my code, becouse in my opition all is not necessary:

Code: [Select]
Char.SetPosition(512, 384);

    Obstacle.SetCenter(Obstacle.GetSize()*.5f);
Char.SetCenter(20, 32); //fits to image size

    Background.Resize(2000,2000);
    Background.Move(-500,-500);
    Obstacle.Move(100,100);
    Obstacle.Scale(2.f, 2.f);

    sf::Vector2f Center(500,500);
    sf::Vector2f HalfSize(App.GetWidth()*.5f,App.GetHeight()*.5f);
    sf::View View(Center,HalfSize);

    sf::Event evt;

    while(App.IsOpened())
    {
      while(App.GetEvent(evt))
      {
         if(evt.Type==sf::Event::Closed){App.Close();}
         if(evt.Type==sf::Event::KeyPressed&&evt.Key.Code==sf::Key::Escape){App.Close();}
if(evt.Type==sf::Event::KeyPressed&&evt.Key.Code==sf::Key::F1)
{
sf::Image Screen;
Screen = App.Capture();
Screen.SaveToFile("sc2.jpg");
}
      }
   
 UpdateBoy(Char, App.GetInput(), Dir, App.GetFrameTime(), go);
      View.SetCenter(Char.GetPosition());

      App.Clear();
      App.SetView(View);
      App.Draw(Background);
      App.Draw(Obstacle);
  App.Draw(Char);

      App.Display();
   }


And image how it looks like:
http://img96.imageshack.us/img96/1225/sc2ki.jpg

If I made as You said(move SetCenter functions after scalling images it looks as that in my previous post.

In my opinion something still is wrong with setting center, but I couldn't find what).

Is that code which I pasted here all right?

Kind Regards
Fuv

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #11 on: June 28, 2010, 03:03:04 pm »
Well, I scaled the sprite before in my original demo and it works perfect. I haven't had any of the problems you are having with the collision in the demo I made.

It's hard to know what's going on without the full code.

Maybe you are not updating with every frame of your animation?

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #12 on: June 28, 2010, 04:00:09 pm »
It's not problem with scalling, becouse if I delete it the problem persist.

I added setting center to loop and it also changed nothing. It seems like the picture is "smaller" few pixels each sides.

I pasted all my code in here:
http://wklej.to/ADnz
There is nearly 300 lines, but most of them are not really connected with that issue and I hope You will catch what is important or not.

I think You don't need animations detalis, but if so there are:
http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated

I have just changed inheritance of class Animated(now it inherits after(?) sf::Sprite - before: sf::Drawable.

I know that it might be hard for You to find what's wrong in stranger code, so I can send You all my project in Visual C++, so You can test what's wrong. If You find time for me of course.


Kind Regards
Fuv

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Connecting scrolling map and collisions
« Reply #13 on: July 02, 2010, 10:05:03 pm »
Yeah it's going to be like a week or two at least until I have time for that.

Fuv

  • Newbie
  • *
  • Posts: 35
    • View Profile
Connecting scrolling map and collisions
« Reply #14 on: July 10, 2010, 01:27:28 pm »
Now I won't be at home till next Sunday, so if somebody will find some time for looking at my code and analyzing it, I will be grateful.

Here I post screenshots that shows how this two objects put on each other:

http://img683.imageshack.us/img683/1760/righto.jpg
http://img192.imageshack.us/img192/2450/leftof.jpg
http://img188.imageshack.us/img188/306/downu.jpg
http://img337.imageshack.us/img337/4456/29414335.jpg

I will be glad for any advices.

Kind Regards