|
Title: Strange Keyboard Input Problem Post by: todd on October 16, 2011, 06:23:51 PM (http://i.imgur.com/HgGdO.png)
I'm trying to change the ball object's direction to the same angle as the player's origin to the point of collision between the bat and the ball. I'm using the grandma engine's hspd and vspd variables and I'm not sure how to determine the correct angle while adding a bit of speed to it. Here's what I have so far: Ball's collision event with the player's bat Code: var pdist; pdist=sign(point_distance(x,y,obj_player.x,obj_player.y))+max(hspd,vspd); hspd=lengthdir_x(pdist,point_direction(obj_player.x,obj_player.y,x,y)); vspd=lengthdir_y(pdist,point_direction(obj_player.x,obj_player.y,x,y)); //adds speed to the ball each hit hspd+=sign(hspd)*1.5 vspd+=sign(vspd)*1.5; //So the ball is always hit to the left if sign(hspd)!=-1 hspd*=-1; I'm getting some strange results from this code. Any help would be greatly appreciated! Title: Re: Object Bounce with the Grandma Engine Post by: Danmark on October 17, 2011, 01:54:10 AM
Title: Re: Object Bounce with the Grandma Engine Post by: lasttea999 on October 17, 2011, 07:53:29 AM How about something like (don't have GM open at the moment, I apologize for any errors):
Code: dx = ball.x - player.x; dy = ball.y - player.y; angle = arctan2(dy, dx); bounceConstant = whatever you want; magnitude = sqrt(hspd * hspd + vspd * vspd); hspd = bounceConstant * magnitude * cos(angle); vspd = bounceConstant * magnitude * sin(angle); Some common (for me, at least) errors with this kind of code that you may want to check, and that may produce errors in the code I wrote: -Signs (positive, negative) -The fact that GM returns or takes degrees with some functions and radians with others Title: Re: Object Bounce with the Grandma Engine Post by: Desert Dog on October 17, 2011, 09:19:33 AM You could just use GM's built in speed/direction, and translate it into your own variables when your done.
collision event Code: x=xprevious; y=yprevious; //not necessarily necessary. //you may want to remove these 2 lines, or even //tweak so that only 1 of them is executed as necessary. direction=point_direction(player.x,player.y,x,y); //I'd always do a 'if instance_exists(player) before //calling this sort of code.. to doubly prevent errors speed=1; hspd=hspeed; vspd=vspeed; speed=0; //maybe multiply hspd, and vspd by some sort of value. As an idea. Title: Re: Object Bounce with the Grandma Engine Post by: Geeze on October 17, 2011, 09:49:45 AM How about something like (don't have GM open at the moment, I apologize for any errors): This should work just fine. All the angles in that code are radians to be exact(checked yoyogames wiki)Code: dx = ball.x - player.x; dy = ball.y - player.y; angle = arctan2(dy, dx); bounceConstant = whatever you want; magnitude = sqrt(hspd * hspd + vspd * vspd); hspd = bounceConstant * magnitude * cos(angle); vspd = bounceConstant * magnitude * sin(angle); Some common (for me, at least) errors with this kind of code that you may want to check, and that may produce errors in the code I wrote: -Signs (positive, negative) -The fact that GM returns or takes degrees with some functions and radians with others I wouldn't recommend Desrt dogs method, because GM does weird syncing between vspeed,hspeed,speed and direction, so it can't be faster (extra degtorads() & vice versa are being run under hood) and doing that kind of stuff by your self is better practice. Title: Re: Object Bounce with the Grandma Engine Post by: todd on October 17, 2011, 12:56:43 PM @ lasttea999 & Geeze - that worked perfectly thank you! I'm more of an art-er. Math is not my strong suit...
Edit: @Veracity - Thanks for giving me the actual math behind it. I need to dedicate some time to studying. Title: Re: Object Bounce with the Grandma Engine Post by: Danmark on October 17, 2011, 05:10:59 PM A couple things to point out about lasttea999's method. In the first 2 lines, he finds the vector from the player to the ball, not the point of collision as you asked for. The ball is probably small enough that it's not important though.
Secondly, the arctan2, cos, and sin can be replaced by one sqrt: Code: dx = ball.x - player.x; dy = ball.y - player.y; magnitude = sqrt(dx * dx + dy * dy); dx /= magnitude; dy /= magnitude; bounceConstant = whatever you want; magnitude = sqrt(hspd * hspd + vspd * vspd); hspd = bounceConstant * magnitude * dx; vspd = bounceConstant * magnitude * dy; It would be visibly simpler if GML had vectors (write your own if possible). Assuming GM has good implementations of sqrt and trig functions, it's certainly far more efficient. It doesn't matter in your case, because there's (presumably) only one player with a bat. However, you want to avoid doing tens of thousands of unnecessary & costly trig functions per second, an easy trap to fall into. Just something to keep in mind. Title: Re: Object Bounce with the Grandma Engine Post by: lasttea999 on October 17, 2011, 07:52:04 PM Oop, sorry about that! Thank you for your refinements, Veracity.
Title: Re: Strange Keyboard Input Problem Post by: todd on October 30, 2011, 07:49:54 PM Wanted to reuse the topic for another issue I'm having.
Here's a script I made to shut off key input when global.play=false Code: //Script named check_key if global.play=true { return(keyboard_check_direct(argument0)) } I use the spacebar to switch this on and off for testing. I use the script in the player object like so Code: if check_key(ord('K')) { atkTimer+=1; charge=true; } The odd thing is if I press K when global.play=false, release, then switch global.play to true, it makes charge=true and atkTimer=1. Why isn't this holding water? |