I've come across video card issues before with EGL (not personally but seen them mentioned) Often fixed by loading latest drivers, good luck with that and let me know how you get on. My wife has a mac but I am STRICTLY forbidden from fiddling with it!
I'm not entirely sure what you intend by the mouse look but after a quick 'flight' I would suggest the following:
don't mix the mouse pointer locations (which are 2D screen pixel related) with the 'real world' (real space) x, y, z positions. This might happen to work in some locations but generally won't.
The point_at needs to point at a real 3D location, by fixing z at 1000 it will look backwards once you move further than that! I think I would make the camera generally point at the spacecraft but maybe have an option that toggles between view and steer (I did something like this with the DogFight demo).
I would do 'proper physics' for steering i.e. apply an up, down, left, right thruster depending on the distance of the mouse pointer from the centre of the screen and have (x,y,z), (dx,dy,dz), (d2x,d2y,d2z) vectors. I would add these as attributes to the ship object rather than having a floating variable i.e. ship.location[0], ship.velocity[0] (NB not speed which doesn't work in space where there's no traction) you also need rotational vectors and their derivatives which you will need to cunningly control also with the mouse position!!
Use a 2D camera for the text. i.e. see attached.
Also I tidied up the MergeShape so you can do
ship.add(ship_core, 0.0, 1.6)
ship.add(ship_ring, 0.0, 1.5)
and it will extract the relevant Buffer list
Code: Select all
#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
""" Use of points by using set_point_size() to a non-zero value. There is
no attempt to 'tile' the points to give continuous motion
"""
import demo
import pi3d,sys
#Spaceship class implemented in the future...
def movement_player(k,ship,mx,my,ship_speed):
if k==112:
pi3d.screenshot("earth1.jpg")
elif k==27:
mykeys.close()
DISPLAY.stop()
DISPLAY.destroy()
#Not sure if this is a nice way to stop
sys.exit()
#Need to create a space movement ish.
#When W is pressed and mouse is at far right top, it needs to turn fast...
#When W is pressed and mouse is close by... it needs to turn sloooowly
elif k == 119: #key, w
#Detect distance from mouse to ship # or center of screen???
#print ('\t'.join([str(mx),str(my),'\n']))
#Move ship
#Speed is increased by 0.1 per tick when pressed
ship_speed[0] += 0.1
#bend_speed_X = (mx / 50 )
#bend_speed_Y = (my / 50 )
#Forward
#ship.positionZ(ship.z() + 0.5)
#ship.positionX(ship.x() + bend_speed_X)
#ship.positionY(ship.y() + bend_speed_Y)
elif k == 115: #key, s #Moving backwards
ship_speed[0] -= 0.1
elif k == 97: #key, a #Flip shift to left or right... camera movement not yet fixed
ship.positionX(ship.x() - 0.5)
#Maybe for Q and E
#ship.rotateIncZ(0.5)
elif k == 32: #Space bar brings ship to halt
if ship_speed[0] > 0.5:
ship_speed[0] -= 0.3
elif ship_speed[0] < -0.5:
ship_speed[0] += 0.3
else:
ship_speed[0] = 0
elif k == 100: #key, d
ship.positionX(ship.x() + 0.5)
#Maybe for Q and E
#ship.rotateIncZ(-0.5)
ship.positionZ(ship.z() + ship_speed[0])
#MAX X Y Z
#Zfwd = 300ms
#Zrev = 100ms
#Y = bend speed?
#Per tick it should increment speed, this should be shown on screen...
return ship, ship_speed
def camera_postioning(CAMERA,mx,my,k):
CAMERA.reset()
CAMERA.position((ship.x() , ship.y(), ship.z()-20))
#Look to where the mouse points at
#Somehow messes up camera positioning.
CAMERA.point_at([mx,my,1000])
if k == 97: #key, a
CAMERA.rotateZ(0.5)
elif k == 100: #key, d
CAMERA.rotateZ(-0.5)
return CAMERA
def mouse_look(mymouse,ship):
#From position of object...
mx, my = mymouse.position()
#SET MAX POS so you cannot look 360*
TOPX = ship.x() + 200
BOTTOMX = ship.x() - 200
TOPY = ship.y() + 200
BOTTOMY = ship.y() - 200
if mx > TOPX:
mx = TOPX
elif mx < BOTTOMX:
mx = BOTTOMX
if my > TOPY:
my = TOPY
elif my < BOTTOMY:
my = BOTTOMY
return mx, my
#from six.moves import xrange
# Setup display and initialise pi3d in full screen mode
DISPLAY = pi3d.Display.create()
DISPLAY.set_background(0,0,0,1) # r,g,b,alpha
# Camera initialization
CAMERA = pi3d.Camera()
CAMERA2D = pi3d.Camera(is_3d=False)
shader = pi3d.Shader("uv_reflect")
flatsh = pi3d.Shader("uv_flat")
#matsh = pi3d.Shader("mat_flat")
earthimg = pi3d.Texture("textures/world_map.jpg")
metalimg = pi3d.Texture("textures/metalhull.jpg")
# Load shapes
earthsphere = pi3d.Sphere(radius=5, slices=24, sides=24,
name="earth", x=10, y=-5, z=180, camera=CAMERA)
earthsphere.set_draw_details(shader, [earthimg])
#create ship
metalimg = pi3d.Texture("textures/metalhull.jpg")
ship_core= pi3d.Sphere(radius=1.0)
ship_ring = pi3d.Cylinder(radius=2.0, height=0.1, sides=12)
ship = pi3d.MergeShape(camera=CAMERA)
ship.add(ship_core, 0.0, 1.6)
ship.add(ship_ring, 0.0, 1.5)
ship.set_draw_details(shader, [metalimg, metalimg], 0.0, 0.5)
ship.position(1, 1, 1)
ship.speed = [0,0] #Fwd/Rev Left/Right
#ship = pi3d.Sphere(radius=2, slices=24, sides=24,
# name="ship", x=0, y=0, z=0)
#ship.set_draw_details(shader, [metalimg])
#myplane = pi3d.Plane(w=500, h=500, name="stars", z=400)
#myplane.set_draw_details(flatsh, [starsimg])
"""create the shape to hold the points. This could be encapsulated in its
own class to generate the required distribution and shield the user from
having to explicitly create the Buffer object and set the Shape.buf list
"""
verts = []
# Fetch key presses
mykeys = pi3d.Keyboard()
mymouse = pi3d.Mouse(restrict=False)
mymouse.start()
# Ship speed hud and more in the future
arialFont = pi3d.Font("fonts/FreeMonoBoldOblique.ttf", (221,0,170,255)) #load ttf font and set the font colour to 'raspberry'
# Display scene
#Game loop
while DISPLAY.loop_running():
mx, my = mouse_look(mymouse,ship)
k = mykeys.read()
CAMERA = camera_postioning(CAMERA,mx,my,k)
#To obtain mouse position for looking at...
ship.draw()
ship.rotateIncY(0.1)
earthsphere.rotateIncY(0.1)
earthsphere.positionZ(earthsphere.z() - 0.0001)
earthsphere.draw()
#Set ship in motion, and if in motion keep moving it! #Its space after all
ship,ship.speed = movement_player(k,ship,mx,my,ship.speed)
#Speed indicator
speedhub = pi3d.String(font=arialFont, string='Speed: {}'.format(ship.speed[0]),
x=-DISPLAY.width/2.0 + 50, y=DISPLAY.height/2.0 - 50, is_3d=False,
camera=CAMERA2D, justify="L")
speedhub.set_shader(flatsh)
speedhub.draw()