Simulating a jump [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am looking to simulate a jump for my Android game, however I have no clue how to achieve this properly. I have been looking up velocity a bit, but I just can't translate it to code myself. Looking for some demo code to get this worked.
The current height variable is called: centerY, this has 427 as default value. When you jump, it has to go to about 360 then go back to 427.
Thanks in advance!

To simplify things I'll assume jumping instantly accelerates the player. Here's some very basic pseudocode of how things should work. Note that you'll need to figure out a better way to handle ground collisions than a simple height check.
G := 9.8 or something other appropriate constant
defaultY := 427;
player {
var centerY
var velocityY
update(dt) {
if (centerY < defaultY)
velocityY += G * dt
centerY += velocityY
else
velocityY := 0
centerY := defaultY
}
jump() {
dh := 427 - 360
velocityY := -sqrt(2 * G * dh)
}
}
In this scheme you'll call player.update on every frame, giving it the time elapsed from the last frame. Note that the velocity is calculated from the second of the kinematic equations:
Which you should definitely know about.

427 - 360 = 67
so, your object need to jump by 67 points.
but, you need gravity and verticalspeed variables of course
so, you've to tell what is the value of your gravity variable

Related

How to make a function to close a door when the player is not near it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So im making a wolfenstein 3d copy in Java and i've done a door list to change from 1 (closed) to 2(open) and i would want to make 2 to 1 when the player is not near it
ive got 2 variables for the player(yPos, xPos) and 2 for the door(row,column)
First of all, I think that "ibm-doors" is not a good tag for your question ^^
Actually, this is a math question. You can calculate the distance between the player and the door with this formula (you may adapt the variable names and operators to your needs):
playerDoorDistance = sqrt((player.xPos - door.xPos)^2) + (player.yPos - door.yPos)^2)
where sqrt is the square root of what is between brackets, and ^2 is the square of what is between brackets.
Once calculated, you can just check that this distance is greater than a value of your choice to close the door.
If you prefer something more simple and that requires less calculation, you can just verify that abs(player.xPos - door.xPos) and abs(player.yPos - door.yPos) are greater than your chosen value (abs is the absolute value of what is between brackets) to close the door. It will verify that your player is outside a squared box around the door.
If you want no math at all, you can check this, which is the same as above (you may also adapt it to your needs):
if (player.xPos - door.xPos > chosenValue || door.xPos - player.xPos > chosenValue || player.yPos - door.yPos > chosenValue || door.yPos - player.yPos > chosenValue)
then closeTheDoor()

Java for every odd multiply of 90 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to create a program which prints something for every odd multiply of 90 so for example
90 - text
180 - nothing happens
270 - text
360 - nothing happens
etc.
Thanks a lot!
A simpler solution is to print the multiples of 180.
for(int i = 180; i < max; i += 180)
System.out.println(i);
I assume this is homework, but there is no point copying someone's answer unless you can explain it. This is for you education purposes. (And your marker will be able to pick up code you probably didn't write yourself)
Your basic structure here should be pretty simple - just use a multiplier and an if statement with a modulus operator.
For example (pseudo-code - check a tutorial if you can't implement this idea):
int multiplier=1;
int maxMultiplier = 10;
int value = 0;
while (multiplier < maxMultiplier) {
value = 90 * multiplier;
if (multiplier % 2 == 0) {
// print something;
}
multiplier++;
}

Java Simulate realistic gravity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How would I go about programming a gravity simulator? I am making a kind of 2d space-simulator and what I want is to have a planet (a center of gravity) to pull objects towards it. The object is a spaceship (basically just x and y-coordinates).
Use Newton's laws - the forces two objects feel are attractive (from one objects mass center point to the other's), equal to each other, and in value equal to g*m1*m2/(d*d) (where g is a constant, m1 and m2 are the masses and d is the distance of the center points.
However, if you have a planet, the effect of the spaceship's attraction to the planet is negligible, so one usually does not do the computations for the planet; it's just stationary.
Keep in mind that F=m*a, where F is the force calculated above, m the mass of the spaceship and a is the acceleration of the object. Based on the acceleration you calculate the speed, and based on the speed the position.
Check out Princeton's N-Body assignment. It describes what you want.
However, in the interest of quick summaries, you can derive the equations from basic trigonometry and Newton's Law of Universal Gravitation:
F = GMm/(r^2)
where F = force between two objects, G = gravitational constant, M and m are the relevant masses, and r is the distance between them.
A little mathemagic and you get the following results:
F_x = F(x_2 - x_1)/r
F_y = F(y_2 - y_1)/r
where F_x is the gravitational force in the x direction (same for F_y, but in y direction), x_2 and y_2 is the position of one of your objects, x_1 and y_1 the position of the other, F is as defined above, and r is the distance between them.

getting the inner corner angle [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have four points that make concave quad:
a(3 , 11)
b(11 , 9)
c(18 , 10)
d(8 , 1)
now i wanna get the inner angle of the (b) corner in degrees.
note: the inner angle is greater than 180 degree.
The safest way is to use inner product and cross product , the inner product can be calculated using the 4 points of the two vectors ( a->b , b->c) and using the inner product formula
<ab,bc> = |ab|*|bc|* cos (abc)
cos(abc) = |ab|*|bc| / <ab,bc>
this is not enough to allocate the angel uniquely since an angle and its complement has the same cos but has different sins , and here is where the cross product comes to solve the problem
ab * bc = |ab|*|bc| * sin(abc)
the left part can be calculated using ending points coordinates so you can calculate the sin , once sin and cos is calculated you can specify the angel appropriately .
Point d is just a distraction here. Consider the (abc) triangle:
It is easy to determine the length of ab, bc and ca from the coordinates.
you can then determine the (ab, bc) internal angle with sin, cos or tan
the angle you look for is 360 minus the (ab, bc) internal angle.

How do I keep the variable BGCD defined in a similar way, but usable in an Arc method? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I apologize about the viewing format. I haven't been able to figure out all the posting details quite yet.
If you Have and recommendations in regards to the formating too, please don't hesitate to tell me.
I'm Drawing a background for a hangman game. I'm working exclusively with visuals.
Eclipse is telling I can only use int methods in the Arc method.
final int XMID = 400;
final int YMID = 300;
//small cirlce diameter
final int SMCD = 60;
final double BGCD = SMCD * 2.5;
// ^^^^^^^^^^ problem
//wave base
g.fillRect(0, 480, 800, 20);
//first big circle (ARC)
g.fillArc(XMID-(SMCD/2) , 480-SMCD , BGCD, BGCD, 0, 130);
// ^^^^^^^^ problem
//first small circle
g.setColor(Color.CYAN);
g.fillOval(XMID-(SMCD/2),480-SMCD , SMCD, SMCD);
// ^^^^^^ problem
You'll need to cast BGCD to an int,
i.e. (int)BGCD because the drawing methods don't work with doubles or floats.
Feel free to post the actual error message next time, it help you get a quick response

Categories

Resources