News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

bouncing object of edge of scrren (sin/cos) related

Started by gingerprince, October 13, 2006, 10:53:58 AM

Previous topic - Next topic

gingerprince

I have a routine to move an object.

I need to test for player moving off edge of screen (or any given boundary),simple enough normally - But I want the player to bounce off the edge of screen (screen edge being angle of incidence,I think) with the same speed/friction etc as the player collided with screen edgebut with NEW correct direction.

Hope this makes sense to someone

Thanks in advance.


;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#


stef

Don't know what you mean; but to "reflect" something you can do it like this

;....
player_x# = player_x# + delta_x#
player_y# = player_y# + delta_y#

if player_x#<0 then delta_x# =delta_x# * -1  ;left screen edge
if player_x#>800 then delta_x# =delta_x# * -1  ;right screen edge
if player_y#<0 then delta_y# =delta_y# * -1  ;upper screen edge
;....