Rebound object of edge of screen?
Blitz3D Forums/Blitz3D Beginners Area/Rebound object of edge of screen?
| ||
I have a routine to move an object (all 2D). Sample below. I need to test for player moving off edge of screen (or any given boudary),simple enough normally - But I want the player to bounce off the edge of screen (screen edge being angle of incidence) with the same speed/friction etc as the player collided with screen edge. Hope this makes sense to someone Thanks in advance. ps.I`m not sure how to add code correctly so I have cut/paste. ;ACCELERATION (PLAYER ACCEL) ADDED TO SPEED VECTOR (DELTA_X#) If playerdirection_u = 1 ;FLAG FOR FORWARD delta_x# = delta_x# + Cos( player_turnangle#) * player_accel# ;PLAYER_TURNANGLE delta_y# = delta_y# + Sin( player_turnangle#) * player_accel# ;IS PLAYER DIRECTION EndIf ;CALCULATE TRUE LENGTH OF SPEED VECTOR (PYTHAG`) speedvectorlength# = Sqr( delta_x# * delta_x# + delta_y# * delta_y#) ;IF MOVING THEN DECREASE SPEED BY FRICTION If speedvectorlength# > 0 delta_x# = delta_x# - (delta_x# / speedvectorlength#) * player_move_friction# delta_y# = delta_y# - (delta_y# / speedvectorlength#) * player_move_friction# EndIf ;IF WE ARE GOING TO FAST THEN REDUCE SPEED TO THE MAXIMUM SPEED If speedvectorlength# > player_max_speed# delta_x# = delta_x# + (delta_x# / speedvectorlength#) * (player_max_speed# - speedvectorlength#) delta_y# = delta_y# + (delta_y# / speedvectorlength#) * (player_max_speed# - speedvectorlength#) EndIf ;UPDATE PLAYERS COORDS player_x# = player_x# + delta_x# player_y# = player_y# + delta_y# |
| ||
A simple version. To add code use ... {code} My code goes here {/code} But change the {} for [] Stevie global GW = graphicswidth() global GH = graphicsheight() [your code] [before update player coords ] CheckX# = PlayerX + delta_x CheckY# = PlayerY + delay_y if CheckX < 0 or CheckX > GW delta_x = - delta_x if CheckY < 0 or CheckY > GH delta_y = - delta_y |
| ||
Thanks for that Steve.....works a treat! ....... the code looks so simple as well. |