[Manic Mack]['Guts'] - calculate force to jump to point :D

Here is a resource and a discussion.

How would you do this differently?
I <3 rocket science :smiley:


import bgefrom mathutils import Vector
from math import cos, sin, sqrt, tan


def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    space = cont.sensors['Space']
    if 'Helper' not in own:
        own['Helper'] = own.scene.addObject('Helper',own,0)
        own['Keep']= own.scene.objects['Keep']
        
        
    if space.positive:
        #create circle objects if they don't exist and jump is held
        if 'C' not in own and own['Charge']&gt;15:
            own['C']=own.scene.addObject('Circle')
            own['C2']=own.scene.addObject('Circle')
            
            #Place jump object
            rot = own.worldOrientation.to_euler()
            rot.y += cos(3.14/4)
            rot.x += cos(3.14/4)
            
            rot = rot.to_matrix()
            x = own['Keep']['y']*.1
            y = own['Keep']['x']*.1
            own['C2'].worldPosition = own.worldPosition+rot*Vector([x,y,-.995])
            own['C2'].color = [1,0,0,1]
            
        if own['Charge']&lt;=99 and own['Time']==0:
            own['Charge']+=1
            own['Time']=1
            V = .5+own['Charge']*.05
            if own['Charge']&gt;15 and 'C' in own:
                own['C'].localScale = [V,V,1]
    elif own['Charge']&gt;15:
        #jump state
        d = own.getDistanceTo(own['C2'])
        if d&gt;0:
            grav = 9.8*9.8
            
            d2 = d*d
            dz = own['C2'].worldPosition.z
            angle = 3.14/4
            
            v0 = (1 / cos(angle)) * sqrt((0.5 * grav* d2) / (d * tan(angle) + dz))
            
            v2 = own.getVectTo(own['C2'].worldPosition)
            own['Helper'].alignAxisToVect([0,0,1],2,1)
            own['Helper'].alignAxisToVect(v2[1],0,1)
            rot = own['Helper'].worldOrientation.to_euler()
            
            rot.y-=3.14/4
            rot = rot.to_matrix()
            own['Helper'].worldOrientation = rot
            p= own['Helper'].worldPosition+rot*Vector([1,0,0])
            
            v2 = own.getVectTo(p)
            own.applyForce(own['Helper'].worldOrientation.col[0]*v0*9.8*2,0)
        
        
        if 'C' in own:
            own['C'].endObject()
            del own['C']
        if 'C2' in own:
            own['C2'].removeParent()
            del own['C2']
                
        own['Charge']=0
        pass
    elif own['Charge']&lt;15:
        own['Charge']=0
     
        
    if 'C2' in own:
        rot = own.scene.objects['Camera'].worldOrientation.to_euler()
        
        
        rot.y += 3.14/2
        rot.z += 3.14/4
        rot = rot.to_matrix()
        x = -own['Keep']['y']*.1
        y = own['Keep']['x']*.1
        if abs(x)&gt;(own['Charge']*.05):
            if x&lt;0:
                x = -own['Charge']*.05
                own['Keep']['y'] = own['Charge']/2
                
            else:
                x = own['Charge']*.05
                own['Keep']['y'] = -own['Charge']/2
                    
        if abs(y)&gt;(own['Charge']*.05):
            if y&lt;0:
                y = -own['Charge']*.05
                own['Keep']['x'] = -own['Charge']/2
            else:
                y = own['Charge']*.05     
                own['Keep']['x'] = own['Charge']/2       
        own['C2'].worldPosition = own.worldPosition+rot*Vector([0,y,x])


main()



Attachments

Jump_to_target_with_charge_circle.blend (453 KB)

Well first of all, it doesn’t seem like the amount of charge affects the distance the cube jumps.

Edit: I don’t get the mini red circle you have.
Edit 2: Ah I have to use wsad to move it.

Ok, so my suggestion: The white charge circle stays put at the origin which makes it a bit confusing to estimate the power when the cube is not at origin. So you should have it be at the cube’s position.

Also I noticed a bug that the cube is able to jump to the red circle even if the red circle is out of the charge.

yeah it can go outside by 1 unit for some reason, I noticed this as well

i fixed the jump radius to be spawned on the agent already (see video)

i will fix the jump range thing (the max needs to be 50 not 51 in the ‘keep’ object)

edit:
fixed

Attachments

Jump_to_target_with_charge_circle_2.blend (456 KB)