Throwin' objects by mouse

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
AlmostNoob
Posts: 13
Joined: Sun Jan 11, 2009 6:34 pm

Throwin' objects by mouse

Post by AlmostNoob »

Hi Norbo. I want to use the mouse to throw a sphere. I would control the sphere by mouse movement, using left button to charging and throwing (when pressed it should charge, when released it should throw). The strength of the Force should be the movement of the mouse (like Tiger Woods videogames, you know...)

Hope you understand.

How should i do?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Throwin' objects by mouse

Post by Norbo »

If the sphere doesn't get picked up as soon as you left click on it but rather sits and waits for you to release the mouse button, you can just raycast each frame that the left mouse is held. When a throwable object is hit by the ray, you store it as the currently grabbed object (you no longer need to raycast). Then, based on the proximity of the projected mouse coordinates (onto a plane placed at the grabbed entity, for example) to the grabbed entity, you change the power and direction. When the mouse button is released, the normalized direction is multiplied by the power and added as an impulse (linearMomentum += impulse would work).

Another option that you could use if you actually pick up the object when the raycast hits instead of it waiting for a release is to monitor the difference in position between each frame. You can translate this difference into a velocity. When the mouse button is released, this last frame velocity is applied to the entity (linearVelocity = velocity would work).

Both of these schemes rely on the mouse to determine the aim which can make things a bit difficult as they will be generally relative to some 2d plane (generally defined by the entity's location and a direction between either the entity and camera's location or the entity and the plane defined by the camera's location and viewing direction).
AlmostNoob
Posts: 13
Joined: Sun Jan 11, 2009 6:34 pm

Re: Throwin' objects by mouse

Post by AlmostNoob »

I've followed your second tip: i've monitored the difference in position between each frame and translated it to velocity. Now i wanna apply an effect proportional to mouse.X movement. I've thinked the best choose was to apply a force to the ball after the launch, but i don't know how to set the force position (1st parameter), should it be the ball position? 'Cause i've setted that way but it didn't work in the right manner... (yes, i've activated isTrackingTarget and isChangingDirectionWhileTracking)

I've also tried to apply a simple Forcefield by creating a huge enough world object (a simple box), but it needs a different simulationIsland for the cylinders pyramid (remember? :D ) and i don't want too many Islands around...
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Throwin' objects by mouse

Post by Norbo »

Regarding the forcefield object, keep in mind that the fieldShape entity shouldn't be added to the space. The forceField system should probably be updated with some of the newer systems in a future version (like the isDetector property of entities or the DetectorVolume). I'm not sure what you mean by the islands.

If you wanted to use a Force to move the ball, the isChangingDirectionWhileTracking should actually be turned off so you can specify the direction without having it rotate on you (you don't want it to be a rocketball, I assume). Keeping isTrackingTarget enabled while starting the force at the ball position would keep the force centered on the ball.

I would recommend however that you simply apply an impulse to the ball for such a controlled, variable force situation. One option is to, for each frame that you are 'holding' the ball, apply an impulse in the proper direction and magnitude. You can just use the linearMomentum field for this, or you could use the applyImpulse/applyLinearImpulse. If you wanted to control the velocity directly without worrying about its mass, you can change linearVelocity.
Post Reply