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

Author Topic: Collision and moving depends on collision point  (Read 6654 times)

0 Members and 2 Guests are viewing this topic.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« on: December 24, 2010, 06:41:20 pm »
Hello I have a scene like that:
http://img121.imageshack.us/img121/8286/grafq.jpg

Red is Your character, blue and black are obstacles.
Now I wanna to detect collision between them(works) and then I want to allow character to move only down if You collide with bottom part of object, move only up when You collide with top part of object and only right when You collide with right part of object and only left when You collide with left part of object. I thought about function which checks with which object You collide and then transfer Sprite to Rect and measure X and Y coords of Character like:
If Character's Y is lower than Bottom of colliding object then  allow to move only down etc.

Is it good solution? Maybe there is better one? By this way rect will be size of FULL picture including transparent pixels >> Should I update Collision function so it will return point of collision and with it I could operate or how to realize it? Or maybe just get center of obstacle and check if You are below it but still upper bottom? And then allow to move only down?

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #1 on: December 25, 2010, 09:40:42 am »
Anyone? My idea is not working quite as I excepted.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #2 on: December 25, 2010, 03:13:38 pm »
Maybe if I show You a graph, it will be easier:

Red dot is center of image, now:
Area 1: Can only move down if collide
Area 2: Can only move right if coolide
Area 3: Can only move right if collide
Area 4: Can only move up if collide
Area 5: Can only move up if collide
Area 6: Can only move left if collide
Area 7: Can only move left if collide
Area 8: Can only move down if collide

The rectangle around triangle shows it full scale, together with transparent pixels. I can't make good logical statements for it to work, especially when trying to deal with area 1 and 2, area 3 and 4 etc because I can't think of good if to make it.

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #3 on: December 26, 2010, 12:28:01 pm »
Anyone?

Okay fuck let's talk another way how can I make sprite not move in direction ahead of obstacle because right now it's shagging like hell.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Collision and moving depends on collision point
« Reply #4 on: December 26, 2010, 03:10:59 pm »
You shouldn't be that impatient at Christmas ;)

I would probably take the vector between the object's center (red dot) and the other, colliding object's center (in the areas). The direction/polar angle of the vector tells you from where the objects collided.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #5 on: December 26, 2010, 03:34:32 pm »
Oh yeah, damn it was simple x_x
So it shoud looks like:


And if Angle will be over or below certain values then I decide where I can move, right? Damn thank You, I was thinking about somethign like this but I was not sure how it should look.

On regular I am patient guy ;) Happy Christmas by the way, lol.

P.S How to calculate it? Well probably i will find it on google so.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision and moving depends on collision point
« Reply #6 on: December 26, 2010, 04:06:25 pm »
If you want an angle, use std::atan2(v.y, v.x). But you can probably use directly the vector's components (especially their sign) for your tests.
Laurent Gomila - SFML developer

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #7 on: December 26, 2010, 04:15:44 pm »
Component of vector 2D is like x value and y value?
Anyway how could I use atan2 with 2 vectors as function takes only 2 paramets?
I want to get an angle.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision and moving depends on collision point
« Reply #8 on: December 26, 2010, 04:19:17 pm »
Quote
Component of vector 2D is like x value and y value?

It's not like, it is ;)

Quote
Anyway how could I use atan2 with 2 vectors as function takes only 2 paramets?
I want to get an angle.

You missed the last step: the direction is the difference between both centers. So we end up with a single vector.
Laurent Gomila - SFML developer

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #9 on: December 26, 2010, 04:21:45 pm »
I just looked throught some pages and found out this:
atan2(v2.y,v2.x) - atan2(v1.y,v1.x), will it give me right angle?

Sorry for kinda chaotic way of posts.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision and moving depends on collision point
« Reply #10 on: December 26, 2010, 04:31:29 pm »
I don't think so. It usually doesn't make sense to calculate an angle from a vector that is not a difference.

Please slow down, read carefully our answers, every step is clearly explained, you don't have to guess anything ;)

Compute the difference first, then apply atan2 and you have your angle. Simple.
Laurent Gomila - SFML developer

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #11 on: December 26, 2010, 04:46:35 pm »
Okay, step by step:
1)Compute center of collision, by divinding height and width of collision area
2)Compute center of object, by dividing it's height and width
3)I substract v1.x and v2.x
4)I substract v1.y and v2.y
5) I put result to atan2 function

Will i be able to detect point for all 8 options?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Collision and moving depends on collision point
« Reply #12 on: December 26, 2010, 05:22:22 pm »
Yep.
Laurent Gomila - SFML developer

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #13 on: December 26, 2010, 05:23:19 pm »
Okay I will do it I hope it's gonna work : >

darekg11

  • Full Member
  • ***
  • Posts: 127
    • View Profile
Collision and moving depends on collision point
« Reply #14 on: December 26, 2010, 06:58:33 pm »
Should I pack that collision area into shape and then do sfShape_TransformToGlobal?

Right now I only get: -2 or -1 angles, damn somewhere something is wrong : >