As far as I know, there’s two functions in BGE called getScreenPosition() and getScreenRay(). The first works by getting the pixel position relative to the camera, and the second works be getting the object hit position relative to in-world units. What I’m wondering is, how would I make a function using these comparable to PyGame’s functions for Width/Height, such as:
import pygame, math
size = width, height = 800, 600
objspeed = [1, 1]
screen = pygame.display.set_mode(size)
events = pygame.event.get()
obj = pygame.image.load("\\obj.png")
while 1:
objrect = objrect.move(objspeed)
if objrect.left < 0 or objrect.right > width:
objspeed[0] = -objspeed[0]
if objrect.top < 0 or objrect.bottom > height:
objspeed[1] = -objspeed[1]
screen.fill(0, 0, 0)
screen.blit(obj, objrect)
pygame.display.flip()
Where the it’s calling the “width” and “height” variables it’s getting from the window width and height to figure out of the object has hit the edge of the window display area, in order to bounce it back in the other direction. In BGE I’ve tried the following code, but it just makes the sphere fly off the screen and never come back:
import bge
import math
import Rasterizer
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner
winWidth = bge.render.getWindowWidth()
winHeight = bge.render.getWindowHeight()
Sphere = scene.objects['Sphere']
Camera = own.scene.active_camera
ballScreenPosition = Camera.getScreenPosition(Sphere)
ballDetect = Camera.getScreenRay(*ballScreenPosition, 100)
if ballDetect == Sphere:
print("Lookit the priddy burdy")
if "init" not in own:
own["ballXSpeed"] = True
own["ballYSpeed"] = True
own["init"] = True
if ballPosition > 1.0:
own['ballXSpeed'] = False
if ballPosition < 0.0:
own['ballXSpeed'] = True
if ballPosition > winHeight:
own['ballYSpeed'] = False
if ballPosition < winHeight:
own['ballYSpeed'] = True
if own['ballXSpeed'] == True:
Sphere.applyMovement((0.1, 0.0, 0.0), True)
if own['ballXSpeed'] == False:
Sphere.applyMovement((-0.1, 0.0, 0.0), True)
if own['ballYSpeed'] == True:
Sphere.applyMovement((0.0, 0.1, 0.0), True)
if own['ballYSpeed'] == False:
Sphere.applyMovement((0.0, -0.1, 0.0), True)
Am I doing something wrong, or are these functions not comparable?