There is no build-in method to do that.
You need to write your own Python controller. But it is not that hard.
I suggest you use a near sensor to identify objects around your force source.
It does not only detect the objects to be affected it also sets up the range of your "force field".
To adjust the strength of the force you can use the optional property "strength". It tells how much the force would be at the location of the source object. It will be linear reduced by the distance of the affected object and the source object. At the outer range the force strength will be 0.0.
Usage:
Place this snippet in a python file force.py
import bge
PROPERTY_STRENGTH = "strength"
DEFAULT_STRENGTH = 1.0
def applyForceFromHere():
if not allSensorsPositive():
return
sensor = getDetectionSensor()
affectedObjects = sensor.hitObjectList
maxRange = sensor.distance
source = getOwner()
strength = getOwner().get(PROPERTY_STRENGTH, DEFAULT_STRENGTH)
for affectedObject in affectedObjects:
sourceToObject = affectedObject.worldPosition - source.worldPosition
direction = sourceToObject.normalized()
ratio = max(0, maxRange - sourceToObject.length) # linear interpolation
force = direction * strength * ratio
affectedObject.applyForce(force)
def getDetectionSensor():
return bge.logic.getCurrentController().sensors[0]
def getOwner():
return bge.logic.getCurrentController().owner
def allSensorsPositive():
for sensor in bge.logic.getCurrentController().sensors:
if not sensor.positive:
return False
return True
Apply following logic to the source object:
- Setup a near sensor (must be the first one) with a distance that covers the area you want to have, ensure to enable [True Level Triggering] at the sensor that activates the force field
- Setup a Python controller in module mode with Module:
force.applyFromHere
- Ensure affected objects are physics objects (dynamic, rigid body, softbody)
How does it work?
When all sensors are positive and any of the sensors triggers the controller the function applyFromHere() will be executed. Each run will apply exactly one force or no force at all. There will be no "duration". Either you trigger the controller or you do no trigger it. Forces will only applied when triggered. That is the reason for [True Level Triggering]. This is not necessary when you do not want to have your force field active.
The near sensor provides the affected objects (only physics objects) within the distance. The Python controller calculates the
- distance to the source object
- direction to away from the source object
- ratio (linear interpolation) with
- 1.0 at the location of the source object
- 0.0 at the distance and further away
- the strength of the force
With that the Python controller applies the individual forces to each single affected object.
Remark: The duration of the force is the atomic minimum (one frame). The Python controller is no actuator. Therefore you need to run it again and again and again to get a continuous force.
Hint: You can enter a negative strength
I hope it helps