Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: [Solved] Got a minor problem  (Read 1727 times)

0 Members and 1 Guest are viewing this topic.

Ryuza

That one guy
[Solved] Got a minor problem
« on: January 24, 2011, 09:57:27 pm »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
Hey all, trying to figure something out here, I've got a simple room with two enemies moving next to each other between two walls.

The first one hits a wall, it bounces back, hits the other and then the problem occurs, for some reason at the exact point of collision they both move one pixel left, one pixel right, and then reverse direction like they're supposed to. It's a bit hard to notice I guess, my brother couldn't see anything wrong with the way they were moving but it seems like a really big and obvious thing to me.

Here's the code for their step event:
Code: [Select]
if place_meeting(x + 1, y, par_solid) ^^ place_meeting(x - 1, y, par_solid) {
     hspd *= -1; // Reverse horizontal direction
}
Hspd is a custom variable used for their movement speed, they have the par_solid object as their parent just for testing, so they'll bounce off each other the same way they'd bounce off of a wall. The only other code involved is establishing [hspd = 1;] in the create event and adding [x += hspd;] in the step event.

Anyone have any ideas as to how to fix this? I've tried a couple other ways of detecting the collision and this was the best one so far.

Edit: Solved thanks once more to Niek.
« Last Edit: January 26, 2011, 08:06:31 am by Ryuza »
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube

Antidote

>.>
Re: [Help] Got a minor problem
« Reply #1 on: January 26, 2011, 01:44:11 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
iirc place_meeting returns non zero when it comes into contact with an object, so that's right however iirc it's also not very reliable.
It's been a while since I've used GameMaker so i can't tell you exactly.
Logged
  • Axiomatic Data Laboratories
Re: [Help] Got a minor problem
« Reply #2 on: January 26, 2011, 06:37:35 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
place_meeting returns false or true, which is rather reliable. It is up to the precision setting of the mask sprite however on how precise it is. I think your problem might be that you are using the XOR operator instead of the OR operator. XOR has the following results:

false ^ false -> false
false ^ true  -> true
true  ^ false -> true
true  ^ true  -> false

This means that when both place_meeting calls return true then you get a false. But it actually all depends on what order you determine the change of direction and when do you actually add to the x position. And if you add hspd a couple of times instead of just once. In all seriousness, no one will be able to help you with just that little bit of coding. The problem might as well be somewhere else.

Another reason for your problem is that you move only one pixel for both of the objects and that at the first frame of collision object A is reversed and B stays the same, because there is no collision registered with the B object. At the second frame A is reversed again and B is also reversed as A actually digs deeper into B now once B checks for collisions. At the third frame A finds another collision and reverses, while when B checks there no longer is a collision due to A's reverse and B remains the same. This problem however is not a coding problem but a design problem.

One solution is that when A detects the collision with B, A also reverses B's hspd. Or that the addition to x += hspd is done in the begin or end step events. Whatever you do, you first have to deal with the colisions of the both of them before you move either one of the two objects. And not first check the collison for A with B (adjust A accordingly) -> move A -> check collision for B with A (adjust B accordingly) -> move B.
« Last Edit: January 26, 2011, 07:37:32 am by Niek »
Logged

Ryuza

That one guy
Re: [Help] Got a minor problem
« Reply #3 on: January 26, 2011, 08:04:35 am »
  • RyuKage2007
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 290
In all seriousness, no one will be able to help you with just that little bit of coding. The problem might as well be somewhere else.

Actually the code I provided was the only code used in the object, establish hspd in create, set up collision and add hspd to x in step.

I moved [x += hspd;] over to the begin step event and it fixed the problem perfectly, I also tested it in end step and got the same result as the begin step. I figured I'd try the first thing you mentioned to fix it and move down the list and that happened to work great.

Anyway, thanks for the help once more Niek :)
Logged
<- Koholint Island - MC Style  <- Link's Awakening Photo Recolors  <- Wind Waker 3D Resources <- Super Mario Bros Crossover
  • RyuKage2007's Youtube
Re: [Solved] Got a minor problem
« Reply #4 on: January 26, 2011, 06:25:34 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Good, now that you know what the problem was, you will never make the same mistake again  :P
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.037 seconds with 47 queries.