[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [36315] branches: There is enough potential developer interest in BGE Components that I' m making a branch for them.

Mitchell Stokes mogurijin at gmail.com
Mon Apr 25 00:11:14 CEST 2011


Revision: 36315
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36315
Author:   moguri
Date:     2011-04-24 22:11:13 +0000 (Sun, 24 Apr 2011)
Log Message:
-----------
There is enough potential developer interest in BGE Components that I'm making a branch for them. I'm also committing my initial work in the area.

Modified Paths:
--------------
    branches/bge_components/release/scripts/modules/bpy/utils.py
    branches/bge_components/release/scripts/startup/bl_ui/space_logic.py
    branches/bge_components/source/blender/blenkernel/CMakeLists.txt
    branches/bge_components/source/blender/blenkernel/intern/object.c
    branches/bge_components/source/blender/blenloader/intern/readfile.c
    branches/bge_components/source/blender/blenloader/intern/writefile.c
    branches/bge_components/source/blender/editors/space_logic/logic_ops.c
    branches/bge_components/source/blender/makesdna/DNA_object_types.h
    branches/bge_components/source/blender/makesdna/DNA_space_types.h
    branches/bge_components/source/blender/makesdna/intern/makesdna.c
    branches/bge_components/source/blender/makesrna/RNA_access.h
    branches/bge_components/source/blender/makesrna/intern/makesrna.c
    branches/bge_components/source/blender/makesrna/intern/rna_internal.h
    branches/bge_components/source/blender/makesrna/intern/rna_object.c
    branches/bge_components/source/blender/makesrna/intern/rna_space.c
    branches/bge_components/source/blender/python/intern/bpy.c
    branches/bge_components/source/gameengine/Converter/BL_BlenderDataConversion.cpp
    branches/bge_components/source/gameengine/Ketsji/CMakeLists.txt
    branches/bge_components/source/gameengine/Ketsji/KX_GameObject.cpp
    branches/bge_components/source/gameengine/Ketsji/KX_GameObject.h
    branches/bge_components/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
    branches/bge_components/source/gameengine/Ketsji/KX_PythonInitTypes.h
    branches/bge_components/source/gameengine/Ketsji/KX_PythonSeq.cpp
    branches/bge_components/source/gameengine/Ketsji/KX_PythonSeq.h
    branches/bge_components/source/gameengine/Ketsji/KX_Scene.cpp

Added Paths:
-----------
    branches/bge_components/
    branches/bge_components/release/scripts/bge_components/
    branches/bge_components/release/scripts/bge_components/camera.py
    branches/bge_components/release/scripts/bge_components/movement.py
    branches/bge_components/release/scripts/bge_components/particle.py
    branches/bge_components/release/scripts/bge_components/vehicle.py
    branches/bge_components/source/blender/blenkernel/BKE_pycomponent.h
    branches/bge_components/source/blender/blenkernel/intern/python_component.c
    branches/bge_components/source/blender/makesdna/DNA_component_types.h
    branches/bge_components/source/blender/makesrna/intern/rna_pycomponent.c
    branches/bge_components/source/gameengine/Ketsji/KX_PythonComponent.cpp
    branches/bge_components/source/gameengine/Ketsji/KX_PythonComponent.h

Added: branches/bge_components/release/scripts/bge_components/camera.py
===================================================================
--- branches/bge_components/release/scripts/bge_components/camera.py	                        (rev 0)
+++ branches/bge_components/release/scripts/bge_components/camera.py	2011-04-24 22:11:13 UTC (rev 36315)
@@ -0,0 +1,74 @@
+import bge
+from collections import OrderedDict
+
+class ThirdPerson(bge.types.KX_PythonComponent):
+	"""A component for a third person camera"""
+	
+	args = OrderedDict([
+		("pPivot Name", "pivot"),
+		("Time Offset", 35),
+		("Lens", 30),
+		("Scale Max", 4.0),
+		("Scale Min", 0.6),
+		("Option", {"One", "Two"})
+		])
+		
+	def start(self, args):
+		# Make sure we have a camera
+		if not isinstance(self.object, bge.types.KX_Camera):
+			raise TypeError("This component must be attached to a camera")
+		
+		print(args['Option'])
+		# Apply settings
+		self.object.parent.timeOffset = args['Time Offset']
+		self.object.lens = args['Lens']
+		
+		# Save scale settings
+		self.scale_max = args['Scale Max']
+		self.scale_min = args['Scale Min']
+		
+		# Find the pivot
+		pivot = self.object.parent
+		
+		while pivot:
+			if pivot.name == args['Pivot Name']:
+				break
+
+			pivot = pivot.parent
+			
+		if not pivot:
+			raise ValueError("Could not find the pivot object")
+			
+		self.pivot = pivot
+		
+		self._target_distance = (self.object.worldPosition - pivot.worldPosition).length
+		
+	def update(self):
+		ob = self.object
+		pivot = self.pivot
+	
+		# Cast a ray and see if we hit something
+		hit_pos = self.object.rayCast(
+					ob.worldPosition,
+					pivot.worldPosition,
+					ob.localPosition[2])[1]
+					
+		if hit_pos:
+			scale = ob.getDistanceTo(hit_pos)/self._target_distance
+			if scale > self.scale_max:
+				scale = self.scale_max
+			elif scale < self.scale_min:
+				scale = self.scale_min
+		else:
+			scale = self.scale_max
+			
+		# Apply the scaling
+		pivot.scaling = [scale, scale, scale]
+		
+		# Undo the scaling on the camera
+		# inv_scale = 1/scale
+		# ob.scaling = [inv_scale, inv_scale, inv_scale]
+		
+		# Update the "look at"
+		vec = ob.getVectTo(pivot)[1]
+		ob.alignAxisToVect(vec, 1)
\ No newline at end of file

Added: branches/bge_components/release/scripts/bge_components/movement.py
===================================================================
--- branches/bge_components/release/scripts/bge_components/movement.py	                        (rev 0)
+++ branches/bge_components/release/scripts/bge_components/movement.py	2011-04-24 22:11:13 UTC (rev 36315)
@@ -0,0 +1,39 @@
+import bge
+
+class ThirdPerson(bge.types.KX_PythonComponent):
+	"""Basic third person controls
+	
+	W: move forward
+	A: turn left
+	S: move backward
+	D: turn right
+	
+	"""
+	
+	args = {
+		"Move Speed": 10,
+		"Turn Speed": 0.04
+	}
+	
+	def start(self, args):
+		self.move_speed = args['Move Speed']
+		self.turn_speed = args['Turn Speed']
+		
+	def update(self):
+		keyboard = bge.logic.keyboard.events
+		
+		move = 0
+		rotate = 0
+		
+		if keyboard[bge.events.WKEY]:
+			move += self.move_speed
+		if keyboard[bge.events.SKEY]:
+			move -= self.move_speed
+			
+		if keyboard[bge.events.AKEY]:
+			rotate += self.turn_speed
+		if keyboard[bge.events.DKEY]:
+			rotate -= self.turn_speed
+			
+		self.object.setLinearVelocity((0, move, 0), True)
+		self.object.applyRotation((0, 0, rotate), True)

Added: branches/bge_components/release/scripts/bge_components/particle.py
===================================================================
--- branches/bge_components/release/scripts/bge_components/particle.py	                        (rev 0)
+++ branches/bge_components/release/scripts/bge_components/particle.py	2011-04-24 22:11:13 UTC (rev 36315)
@@ -0,0 +1,116 @@
+import bge
+import mathutils
+import math
+import random
+from collections import OrderedDict
+
+X_AXIS = mathutils.Vector((1, 0, 0))
+Y_AXIS = mathutils.Vector((0, 1, 0))
+Z_AXIS = mathutils.Vector((0, 0, 1))
+
+class ParticleSystem(bge.types.KX_PythonComponent):
+	"""Simple particles System"""
+	
+	args = OrderedDict([
+		("Particle Name", ""),
+		("Particles Per Frame", 1),
+		("X Angle", 30.0),
+		("Y Angle", 30.0),
+		("X Size", 0.0),
+		("Y Size", 0.0),
+		("Starting Velocity", 500.0),
+		("Velocity Variance", 0.0),
+		("Particle Life", 30),
+		("Life Variance", 0.0),
+		("Gravity", -9.8),
+		])
+		
+	def start(self, args):
+		self.valid = True
+		self.particle = args['Particle Name']
+		self.part_life = args['Particle Life']
+		self.ppf_inv = 1/(args['Particles Per Frame'] if args['Particles Per Frame'] else 1)
+		
+		# Save directional variance as radians
+		self.x_var = math.radians(args['X Angle'])
+		self.y_var = math.radians(args['Y Angle'])
+		
+		# Save the offests
+		self.x_off = args['X Size']
+		self.y_off = args['Y Size']
+		
+		# Save variances
+		self.velocity_var = args['Velocity Variance']
+		self.life_variance = args['Life Variance']
+		
+		# Store a time step
+		self.dt = 1/bge.logic.getLogicTicRate()
+		
+		# Precalculate gravity*dt
+		self.gravity_dt = args['Gravity']*self.dt
+		
+		# Precalculate velocity*dt
+		self.start_velocity_dt = args['Starting Velocity'] * self.dt
+		
+		# Add the first particle into the list
+		self.particle_list = [self.create_particle()]
+		
+	def update(self):
+		frame_position = self.ppf_inv
+		while frame_position <= 1:
+			self.particle_list.append(self.create_particle(frame_position))
+			frame_position += self.ppf_inv
+			
+		[self.update_particle(particle) for particle in self.particle_list]
+			
+	def create_particle(self, frame_position=0):
+		# Add the particle
+		scene = bge.logic.getCurrentScene()
+		particle = scene.addObject(self.particle, self.object, 0)
+		
+		# Determine particle heading
+		x_tilt = random.uniform(-self.x_var, self.x_var)
+		y_tilt = random.uniform(-self.y_var, self.y_var)
+		
+		# Apply x offset
+		x_dir = self.object.getAxisVect(X_AXIS)
+		particle.worldPosition = particle.worldPosition.copy() + x_dir * random.uniform(-self.x_off, self.x_off)
+		
+		# Apply y offset
+		y_dir = self.object.getAxisVect(Y_AXIS)
+		particle.worldPosition = particle.worldPosition.copy() + y_dir * random.uniform(-self.y_off, self.y_off)
+		
+		# Determine the particle velocity vector
+		velocity = self.object.getAxisVect(Z_AXIS)
+		velocity.rotate(mathutils.Euler((x_tilt, 0, 0)))
+		velocity.rotate(mathutils.Euler((0, y_tilt, 0)))
+		
+		# Assign the particle properties
+		particle['life'] = self.part_life
+		if self.life_variance > .0001:
+			particle['life'] *= 1+random.uniform(-self.life_variance, self.life_variance)
+		particle['velocity'] = velocity * self.start_velocity_dt
+		if self.velocity_var > .0001:
+			particle['velocity'] *= 1+random.uniform(-self.velocity_var, self.velocity_var)
+		# Deal with subframe positioning
+		if frame_position < 1:
+			particle.worldPosition = particle.worldPosition.copy() + particle['velocity']*self.dt*frame_position
+		
+		# Return the particle
+		return particle
+		
+	def update_particle(self, particle):
+		# Update particle life
+		if particle['life'] == 0:
+			self.particle_list.remove(particle)
+			particle.endObject()
+			return
+		else:
+			particle['life'] -= 1
+			
+		# Apply gravity to the particle's velocity
+		particle['velocity'][2] += self.gravity_dt
+		
+		# Update particle position
+		particle.worldPosition = particle.worldPosition.copy() + particle['velocity']*self.dt
+		

Added: branches/bge_components/release/scripts/bge_components/vehicle.py
===================================================================
--- branches/bge_components/release/scripts/bge_components/vehicle.py	                        (rev 0)
+++ branches/bge_components/release/scripts/bge_components/vehicle.py	2011-04-24 22:11:13 UTC (rev 36315)
@@ -0,0 +1,129 @@
+import bge
+from collections import OrderedDict
+
+class Vehicle(bge.types.KX_PythonComponent):
+	"""A component making use of the vehicle wrapper
+	
+	Controls:
+	W: Move forward
+	S: Move backward
+	A: Turn left
+	D: Turn right
+	SPACE: Brake
+	
+	"""
+	
+	args = OrderedDict([
+		("Gas Power", 15.0),
+		("Reverse Power", 10.0),
+		("Brake Power", 10.0),
+		("Turn Power", 0.3),
+		
+		("Front Wheel Drive", True),
+		("Rear Wheel Drive", False),
+		("Tire Prefix", "tire_"),
+		("Front Tire Radius", 0.3),
+		("Rear Tire Radius", 0.3),
+		("Tire Friction", 10.0),
+		("Suspension Height", 0.2),
+		("Suspension Compression", 6.0),
+		("Suspension Damping", 1.0),
+		("Suspension Stiffness", 20.0),
+		("Roll Influence", 0.06),
+		])
+	
+	def start(self, args):
+	
+		# Save power settings
+		self.gas = args['Gas Power']
+		self.reverse = args['Reverse Power']
+		self.brake = args['Brake Power']
+		self.turn = args['Turn Power']
+		
+		# Save steering settings
+		self.fwd = args['Front Wheel Drive']
+		self.rwd = args['Rear Wheel Drive']
+		
+		# Create the vehicle constraint
+		constraint = bge.constraints.createConstraint(self.object.getPhysicsId(), 0, 11)
+		cid = constraint.getConstraintId()
+		vid = bge.constraints.getVehicleConstraint(cid)
+		self.vid = vid
+		
+		# Find the tires (they should be parented)
+		tpx = args['Tire Prefix']
+		tires = [None]*4
+		for child in self.object.childrenRecursive:
+			for i in range(4):
+				if child.name.startswith(tpx+str(i+1)):
+					tires[i] = child
+					
+					# Unparent the tire so it doesn't cause the vehicle wrapper grief
+					child.removeParent()
+
+		# Verify that we have all of the tires
+		for idx, tire in enumerate(tires):
+			if tire is None:
+				raise ValueError("Tire "+str(idx+1)+" not found")
+				
+		# Now setup the tires
+		for i in range(4):			
+			# Add the wheel
+			vid.addWheel(
+					# Object
+					tires[i],
+					
+					# Position
+					tires[i].worldPosition - self.object.worldPosition,
+					
+					# Suspension angle
+					(0, 0, -1),
+					
+					# Suspension axis
+					(-1, 0, 0),
+					
+					# Suspension height
+					args['Suspension Height'],
+					
+					# Tire radius
+					args['Front Tire Radius'] if i in (0, 1) else args['Rear Tire Radius'],
+					
+					# Steerability
+					args['Front Wheel Drive'] if i in (2, 3) else args['Rear Wheel Drive'])
+					
+			# Advanced settings
+			vid.setTyreFriction(args['Tire Friction'], i)
+			vid.setSuspensionCompression(args['Suspension Compression'], i)
+			vid.setSuspensionDamping(args['Suspension Damping'], i)
+			vid.setSuspensionStiffness(args['Suspension Stiffness'], i)
+			vid.setRollInfluence(args['Roll Influence'], i)
+					
+	def update(self):
+		keyboard = bge.logic.keyboard.events
+		# Engine force

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list