Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411500 Posts in 69379 Topics- by 58435 Members - Latest Member: graysonsolis

April 30, 2024, 03:26:18 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Game Maker for Beginners: Part III
Pages: 1 ... 3 4 [5] 6 7 ... 10
Print
Author Topic: Game Maker for Beginners: Part III  (Read 129053 times)
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #80 on: February 23, 2009, 01:10:01 PM »

Weird, why would adding additional collision checking cause it to miss more? Did you remove the other forms of collision detection? (It's good to keep both.)

One thing I can suggest you try is that you don't use xprevious, instead do something like:

collision_line(x, y, old_x, old_y, 1, 1)
old_x = x; old_y = y;

As an aside, you don't need "self" because it's redundant, anything automatically refers to itself, so self.variable and variable are always going to be exactly equivalent, so it just takes up space.
Logged

ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #81 on: February 23, 2009, 01:11:58 PM »

Oh, and initialize old_x and old_y to x and y in the create event if you do that, otherwise the first old_x and old_y will be 0, leading to bad results. Make sure they're not variables local to that function though (i.e. don't declare them every step), since they do need to retain their values between steps.
Logged

Ataraxia
Level 0
**


View Profile
« Reply #82 on: February 23, 2009, 01:15:50 PM »

And I should put:
old_x = x; old_y = y;

in the End Step event correct??
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #83 on: February 23, 2009, 01:22:36 PM »

No, you can just put it after you use old_x and old_y -- i.e. at any point after the collision_line line.
Logged

Ataraxia
Level 0
**


View Profile
« Reply #84 on: February 23, 2009, 01:29:24 PM »

Hahaha, I get it now. *bashes head against table*

Thanks for the help! The problem was that I had disabled my old method of collision detection, foolishly thinking that the new method would be enough. Everything seems to have worked out perfectly and I don't get the problem anymore. I'm thinking I might post what I've got in awhile to get some general criticisms and suggestions. It is my first game after all.
Logged
salade
Level 4
****



View Profile
« Reply #85 on: February 23, 2009, 01:30:36 PM »

this is a bit more advanced, but you could also use for loops.

Logged
Ataraxia
Level 0
**


View Profile
« Reply #86 on: February 23, 2009, 11:39:45 PM »

Okay I've got another question for yea guru's. I wrote some code to check whether a respawn position for the player is free or not. I'm having two problems with it; First, the code will sometimes fail and the player will spawn in a wall. Also, if the player is able to respawn in two positions it will always choose the one with the lowest y-value. I want it to choose the closest, is there any way to make the two for loops run parallel to one another? Any ideas? Here's my code (it's placed on an an object that I create where the player would spawn if there were no obstacles):
Code:
//check if placed in an obstacle

if place_free(x,y) = false
{
for (yfreeup = view_hview / 2; yfreeup > 0; yfreeup -=1)
{
if (place_free(x + self.sprite_width,yfreeup))
    {
    instance_create(x,yfreeup - 10,oPlayerShip);
    instance_destroy();
    break;
    }

}
for (yfreedown = view_hview / 2; yfreedown < view_hview; yfreedown +=1)
{
if (place_free(x + self.sprite_width,yfreedown))

    {
    instance_create(x,yfreedown + 10,oPlayerShip);
    instance_destroy();
    break;
    }
}
}
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #87 on: February 24, 2009, 08:31:43 AM »

I don't know exactly how to solve your problem, but here is the code I use to accomplish the same thing in my current project. Maybe you could adapt it. Make sure you declare the variables if you don't have 'treat uninitialized variables as 0'. I know it's not a good practice to have that checked, but I do it since I think it saves me time overall.

collision_freeme() - frees the current object from a wall if they're inside one

Code:
while (not collision_free())
{
   col_dir = random(360);
   x = x + lengthdir_x(testlength, col_dir);
   y = y + lengthdir_y(testlength, col_dir);
   testlength += 1;
}

collision_free() - used by the above

Code:
col_free = YES;
col_obj = 0;

with(all)
{
   if (object_index == obj_float)
      if (point_distance(x + width/2, y + height/2, other.x, other.y) < width/2 + 10)
         {other.col_obj = id; other.col_free = NO;}
   if (object_index == obj_wall) or (object_index == obj_creature_) and (id != other.id)
      if (other.x <= x + width) and (other.x >= x)
         if (other.y <= y + height*0.8) and (other.y >= y)
            {other.col_obj = id; other.col_free = NO;}
}
return (col_free);


Logged

Ataraxia
Level 0
**


View Profile
« Reply #88 on: February 24, 2009, 08:42:34 AM »

That seems like it will work perfectly, I just need to adapt it to only check points on the y-axis. Thanks again Paul, I always learn about a new function when reading your examples.
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #89 on: February 24, 2009, 08:48:29 AM »

One thing I just noticed there that you may need to watch out for:

'obj_float' objects are usually "circular" so I use a circular collision detection with them, whereas 'obj_wall' objects are usually rectangular so I use a bounding box. You could probably replace 'width' and 'height' with the bounding box width and bounding box height or the sprite width and sprite height, but in that game I set a variable called 'width' and 'height' for each instance in its creation event, they aren't default properties so you'd need to account for that.
Logged

ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #90 on: February 24, 2009, 08:50:10 AM »

Also the reason I reduce height by 80% and use width normally is to account for perspective: it makes sense to allow a little leeway for moving in front of and behind objects if the perspective isn't perfectly top-down. My game's perspective is something like this:



So if your game's perspective differs from that you would probably remove the *0.8
Logged

sandcrab
Level 0
***


Buzzing!


View Profile WWW
« Reply #91 on: February 24, 2009, 09:55:56 AM »

Sooo, my game has become something completely different now (I can't even pretend anymore!) so please let me know if I ought to move this to another thread. Smiley  Now it's a thrust-like gravity shooter, where the player can bounce off walls, or, if moving slowly enough and positioned at an angle close to 90 (straight up) can land snugly on the ground.

I have two problems.  First, I got my "landing" code from here, following the advice given in the thread.  Then I modified it so that it only worked when oriented correctly (otherwise you just fall through the floor, and it seemed to be working fine.  All this is in the player object's step event.  Then I created a separate collision event where I just put in the basic bounce function.  When I created the separate collision event, my character began to jitter up and down when he landed.  I don't know what else is going on here, but now if I take the collision away, he still jitters.  Here's that bit of code:

Code:
if (place_free(x, y+vspeed+1))
    {
    gravity = playerGravity;
    }
    else
        {
        if (speed < landingSpeed) && (thrustDirection > 75) && (thrustDirection < 105)
            {
            y = (instance_place(x, y+vspeed+1, oBlock)).bbox_top-(bbox_bottom-y);
            gravity = 0;
            speed = 0;
            thrustDirection = 90;
            landed = true;
            }
        }

Second, the reason I took the bounce away is because it was giving unpredictable results, sometimes bouncing in the opposite direction I intended (like if he were skipping across the floor, he would suddenly bounce back in the direction he came).  I want a more predictable BreakOut-style bounce, so I tried making my own in the player's step event.  This is my first bit of code on this, which should only (correctly) do bounces off walls that I come at horizontally with a direction between 0 and 90 degrees (figured I'd do it one step at a time), but instead I ALWAYS bounce off heading at 180 (sometimes it even effects him while jittering in place).  What should I really be doing?

Code:
if (!place_free(x+hspeed+1, y))
    {
    speed = speed / 2;
    x = xprevious;
    if (direction >= 0) direction += 180 - direction;
    //if (direction < 270) direction += 180 - direction;
    thrustDirection = direction;
    }

many thanks!
Logged

Ataraxia
Level 0
**


View Profile
« Reply #92 on: February 24, 2009, 10:17:11 AM »

Also the reason I reduce height by 80% and use width normally is to account for perspective: it makes sense to allow a little leeway for moving in front of and behind objects if the perspective isn't perfectly top-down. My game's perspective is something like this:



So if your game's perspective differs from that you would probably remove the *0.8

Okay so I've successfully adapted the code you gave me to my game. Unfortunately it has the exact same problem my previous code had, sometimes the player still spawns in a wall. The good news is that I have determined what causes that to happen! My player sprites origin is at (0,0). This gives me a problem when the position of the origin is outside of the wall, but the players bounding box will still overlap the wall. How did you account for this in your code?
Logged
Ataraxia
Level 0
**


View Profile
« Reply #93 on: February 24, 2009, 10:34:23 AM »

Code:
if (place_free(x, y+vspeed+1))
    {
    gravity = playerGravity;
    }
    else
        {
        if (speed < landingSpeed) && (thrustDirection > 75) && (thrustDirection < 105)
            {
            y = (instance_place(x, y+vspeed+1, oBlock)).bbox_top-(bbox_bottom-y);
            gravity = 0;
            speed = 0;
            thrustDirection = 90;
            landed = true;
            }
        }

Just going to try and take a stab at this one. Rather than setting gravity = 0 have you tried setting playerGravity = 0? It seems to me that even though the ship has landed and gravity has been set to 0 it resets the gravity to playerGravity in the next step. Also you could try adding '&& (landed = false)' to 'if (speed < landingSpeed) && (thrustDirection > 75) && (thrustDirection < 105)'. This way if the player is landed, your code won't try to 're-land' the player. I hope that helps, bare in mind I'm also a noob so that may not work at all Wink
Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #94 on: February 24, 2009, 12:04:59 PM »

I think my origin is 'center' rather than 0,0. If you want to use 0,0, what you could do is adjust the scripts so that everywhere where it uses 'x' or 'y' it instead uses x + width/2 and y + height/2 (assuming you have width and height defined).
Logged

ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #95 on: February 24, 2009, 12:07:02 PM »

Actually I think my wall and float objects do use 0,0 as their origin. It's just the mobile things like the player's ship and the creatures that use the center as their origin. So don't change the wall's x to x + width/2, instead just change the thing being moved to that.
Logged

Ataraxia
Level 0
**


View Profile
« Reply #96 on: February 24, 2009, 12:16:57 PM »

Oh man, why do I always have to over complicate things in my head. That was simple. Thanks again Paul. Problem solved. There's a few things I'm going to be doing different next time I start a game. While time to design a boss and then I'm going to post it up for some criticism Smiley

EDIT: Jeeze! So after further testing, I found the script still didn't quite work perfectly. So a few hair strands pulled from my head later I came up with the solution. Rather than checking my players center, I check the top-left corner and bottom-right corner. This way no matter what the player will not be re-spawned unless the re-spawn point is completely free of objects for the player height and width. Well, atleast I learned a lot. Here's the code for anyone interested:
Code:
col_free = true;
col_obj = 0;

with(all)
{
   if(object_index == oWall) or (object_get_parent(object_index) == oWall)
   {
      if ((other.x + other.sprite_width) <= x + sprite_width) and ((other.x + other.sprite_width) >= x)
      {
        if ((other.y + other.sprite_height)<= y + sprite_height) and ((other.y) >= y)
        {other.col_free = false;}
      }
       
        if ((other.y)<= y + sprite_height) and ((other.y + other.sprite_height) >= y)
        {
             
            if ((other.x) <= x + sprite_width) and ((other.x) >= x)
            {other.col_free = false;}
        }
    }
}
return (col_free);
« Last Edit: February 24, 2009, 12:48:56 PM by Ataraxia » Logged
ஒழுக்கின்மை (Paul Eres)
Level 10
*****


Also known as रिंकू.


View Profile WWW
« Reply #97 on: February 24, 2009, 02:15:24 PM »

That's a nice method, I'll have to adapt that to my game too if I get problems with mine not working (although as far as I know I have never had it get me stuck in a wall yet, but I haven't tested it as fully as I should).
Logged

Ataraxia
Level 0
**


View Profile
« Reply #98 on: February 24, 2009, 03:35:38 PM »

Quote
Second, the reason I took the bounce away is because it was giving unpredictable results, sometimes bouncing in the opposite direction I intended (like if he were skipping across the floor, he would suddenly bounce back in the direction he came).  I want a more predictable BreakOut-style bounce, so I tried making my own in the player's step event.  This is my first bit of code on this, which should only (correctly) do bounces off walls that I come at horizontally with a direction between 0 and 90 degrees (figured I'd do it one step at a time), but instead I ALWAYS bounce off heading at 180 (sometimes it even effects him while jittering in place).  What should I really be doing?

Code:
if (!place_free(x+hspeed+1, y))
    {
    speed = speed / 2;
    x = xprevious;
    if (direction >= 0) direction += 180 - direction;
    //if (direction < 270) direction += 180 - direction;
    thrustDirection = direction;
    }

many thanks!

Thought about this a little bit today. I think the exact function you are looking for is motion_add(). Read up a bit about it in the manual and you should have an idea of how to use it. It's vector addition so you could use it to add the ships current vector to say a vector of half the ships speed and the ships opposite direction. Hope that helps.
Logged
sandcrab
Level 0
***


Buzzing!


View Profile WWW
« Reply #99 on: February 25, 2009, 09:45:29 AM »

Thanks for the suggestions, but they were a no-go.

Motion_add is what I use for the basic thrusting around, it shouldn't work for bouncing because it doesn't have an immediate effect and causes your momentum to shift gradually.  If I just "add" motion, it won't take away the existing motion.  That's why I tried to just reverse the direction entirely. 

I also tried reversing hspeed, and that works pretty well for bouncing, but it really, really disagreed with my landing code, causing the player to flip upsidedown and twitch all the way off bottom of the screen. xD
Logged

Pages: 1 ... 3 4 [5] 6 7 ... 10
Print
Jump to:  

Theme orange-lt created by panic