|
Written by CR Team
|
|
Friday, 07 August 2009 17:45 |
|
In one of our previous article we talked about the use of BaMove xtra and use of mouse in 3d world. So how does one go about using the same to move a rigid body ( take the famous Arknoid game paddle as an example ). Much like the game 'Pong', the player controls the "Vaus", a space vessel that acts as the game's "paddle" which prevents a ball from falling from the playing field, attempting to bounce it against a number of bricks. -Wikipedia 
First we create the rigid body ... ( see the tutorials section ).
Then to initialize/synch the cursor and the rigid body ( the paddle in the this case ).
The following code snippet should do that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
--some global variable to keep track of things global p3d_worldPoint,mouse_click on mouseDown me --get the sprite handler pstage = sprite(me.spritenum) --physx is the name we gave to our physics member physX = member("physX") --3dworld , name of the 3d member to which physics id attached p3dMember = member("3dworld") --mouse_click, just a flag to avoid multiple clicks, set initially to 0 if mouse_click <> 1 then rb_paddle = physX.getrigidbody("paddle_rb") --make sure paddle rigid body is present if rb_paddle <> void then --set the mouse_click variable to 1, so multiple clicks are avoided mouse_click = 1 --hiding the cursor _player.cursor(200) --get the mouse location on the sprite lc_mouse_points = (the mouseloc) - point(pstage.left, pstage.top) --get the current position of the paddle p3d_worldPoint = pstage.camera.worldSpaceToSpriteSpace(rb_paddle.position) --sync the mouse position to that of the rigid body baMoveCursor( p3d_worldPoint.locH, p3d_worldPoint.locV ) end if end if end
|
Now time to get the user input right when he uses the mouse !
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
global mouse_click,p3d_worldPoint --inside some function... if mouse_click <> 0 then lc_mouse_points = (the mouseloc) - point(pstage.left, pstage.top) diff = lc_mouse_points - p3d_worldPoint rb_paddle = physX.getRigidBody("paddle_rb") diff_horizontal = diff.locH diff_vertical = diff.locV --some logic to restrict your paddle going beyond some range --x-axis in this case if (diff_horizontal > MAX_+ve_X) then diff_horizontal = 350 end if if (diff_horizontal < MAX_-ve_X) then diff_horizontal = -350 end if --the following parameters may need tweaking based on your world axis rb_paddle.position = vector(diff_horizontal,diff_vertical,rb_paddle.position.z) end if
|
Thats it ! Your rigid body should follow the mouse pointer now with ease. The above logic can be used for a lot of other scenarios.
Explore them and let us know as well !!
|