(SOLVED) Need Help with collision coding

Hello! I'm making a game with randomized level generation using the tutorial by Heartbeast (link below):

The video is part 2 in his series, it's about collision with the randomized background walls. In the video, he uses keyboard controls for his player, setting them to variables (hspd and vspd) which allow him to collide with the background in his code; but I wish to use mouse controls for movement, which I've gotten to work just fine! To learn how to implement mouse controls, I watched the wonderful tutorial by Aerion (link below):

Now, both work just fine, but I really want mouse controls for this game and so I'll need to know how to code collision for the mouse movement with the background walls. And yes, I am using the exact codes for the mouse controls and level generation/collision from the videos. Any help would be greatly appreciated! :)

PS: If I can just make slight changes to Heartbeast's collision code instead of typing up a new one, that would be preferred!
 
R

Rosieu

Guest
Hi Luke,

I'll preempt this by saying that it's been ages since I've been at GML and Game Maker in General, so I could be a bit rusty!

I think you may have some luck if you rework the movement script that HeartBeast set up. At around
this time of his video (5:12 in if the link doesn't work), HeartBeast sets up the movement by defining the hspd and vspd as the difference between keyboard presses on the arrows. I THINK if you define those two variables in terms of the speed the object is approaching the location of the mouse, you could be on the right track.

I should note that this depends on how you object moves to the mouse. If your object angles itself towards the mouse, then moves, look at comparing the speed of the object and using it here:
(22:51 in) in place of the two separate collision checks.

I implore a more experienced user to check that I'm not talking totally out of nowhere here, I'm basing these recommendations on limited knowledge of GML and a logic process far more based in Construct 2 logic.

Sorry I can't be of more help!
 
thank you so much for your advice! I'll look into it, but I'm honestly not sure how that would even look in code, since I'm very new to it all.
No, my object does not angle itself before moving. If actually getting collision set up is too advanced, I think all I need to figure out how to stop the player from clicking on the wall background or somehow bump the obj_mouseclick off of the wall background and onto the nearest floor background instead, which is ideal because then the player doesn't simply stop moving altogether and still doesn't enter the wall. If someone could show me how to do that, that would be super super awesome!!
That is, unless doing something like that instead of figuring out the collision between the player object and the wall background will make AI pathfinding much harder, in which case I would rather learn how to fix the actual collision.
If the walls were objects it seems like it would be a lot more simple. Again, a huge thank you to anyone who is able to help me solve this problem!! :D
 
R

Rosieu

Guest
If the walls were objects it would make collisions a whoooooolllleeee lot easier to work out, especially if you're new to it (I feel your pain there! :) ).

You could certainly try bouncing your obj_mouseclick off the walls, but to my (again, very limited) knoweldge, that would require the walls to be objects. XD
 
yeah seriously!! Well, the reason why they're not objects is because if there was a ton of objects in the level, it would lower the frame rate. So having the walls apart of the background helps with that but makes coding collision much harder :( I would love if someone could try scripting a way to resolve this issue and post it here for me to try! :D
 
A

Azure Spectre

Guest
Sorry, I don't have enough time to watch both videos, but I'll try and help.

So I've modified Heartbeast's keyboard input variables into mouse direction variables, but there is still work to be done here. Hopefully this gets you started.

Code:
///Get Input and Move
var spd=3;

var hspd=lengthdir_x(spd,point_direction(obj_player.x,obj_player.y,mouse_x,mouse_y));
var vspd=lengthdir_y(spd,point_direction(obj_player.x,obj_player.y,mouse_x,mouse_y));

//Move
move(hspd,vspd);
Edit:
Maybe on a mouse click you could create an obj_moveflag at mouse_x and mouse_y. Then when the player hits a wall or the flag, destroy the flag. In that case the above code would look like this:
Code:
///Get Input and Move
var spd=3;

var hspd=lengthdir_x(spd,point_direction(obj_player.x,obj_player.y,obj_moveflag.x,obj_moveflag.y));
var vspd=lengthdir_y(spd,point_direction(obj_player.x,obj_player.y,obj_moveflag.x,obj_moveflag.y));

//Move
move(hspd,vspd);
 

Jakylgamer

Member
this will allow you to move while the mouse button is held down, if thats what you want
Code:
var pdir = point_direction(x,y,mouse_x,mouse_y);//face the mouse

if (!mouse_check_button(mb_left)) {
    mspd = 0;///not moving
} else
{mspd=1;//moving}

var hspd = lengthdir_x(mspd,pdir);
var vspd = lengthdir_y(mspd,pdir);

movement(hspd,vspd);
 
Thanks Azure, I already have mouse controls but that script may help me with what I need. I already have it creating an object on the position where the mouse is and the player object moves to that position. That second code looks great for removing the obj_mouseclick, but would it be destroyed if obj_mouseclick was positioned on a wall (which is a background) using that code?

Thanks Jakylgamer! Now I can hold down the button to move :)

But, I need to figure out the collision. This is the collision HeartBeast has in his video:

Code:
/// Move (hspd , vspd)
var hspd = argument[0];
var vspd = argument [1];

// Horizontal collisions
if (grid_place_meeting(x+hspd, y)) {
    while (!grid_place_meeting(x+sign(hpsd), y)) {
        x+=sign(hspd);
    }
    hspd = 0;
}

// Move horizontally
x+=hspd;

//Vertical collisions
if (grid_place_meeting(x, y+vspd)) {
    while (!grid_place_meeting(x,y+sign(vspd))) {
        y+=sign(vspd);
    }
    vspd = 0;
}

// Move vertically
y+=vspd;
But I need those collision codes to work with my mouse controls. Hope posting the actual collision code helps you guys figure out how to change it to collide with the mouse controls. Not sure if it should collide with the obj_mouseclick or obj_hero (which is the player object). Thank you all for your help!! :) Hope we can figure this out soon!
 
ohhhh, so Jakylgamer, what you're saying is that the code you posted should actually go under Scripts instead of a code action within a step event within obj_hero? And you're saying the last line where you said movement(hspd,vspd); should actually be the script name, not just part of the code at the bottom?
 
hmmm just tried your script Jakylgamer, doesn't seem to have worked. My character still moves through the grid walls.
Also that part that says:

Code:
} else
{mpsd=1;//moving}
the last bracket thing (I forget the name :D) turns green as if part of a title instead of part of the code, so all my lines after that have an error saying "symbol expected" because it doesn't count the green bracket thing as ending the {mspd=1; (I tried just adding a bracket thing at the end of the semicolon here
 

Jakylgamer

Member
oh sorry its
else {
mspd=1;//moving
} <just forgot to move the bracket down a line :p

so its not working after you did that wither tho?
 
Nah it's not working :( my character still walks through the walls like their nothing. Thanks for the help so far, I don't know if you've watched both videos yet, but in the by HeartBeast, he does the grid. I don't know if this is normal for grid walls, but they're actually backgrounds so that it's not a ton of objects all over the place. If you already read or watched that, sorry for repeating the information, but I hope it helps people more experienced than I am come up with the conclusion! :D
 
Top