Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Game Mechanics / How to reflect an object, with constant acceleration, when hitting a circle?
|
on: 2012-10-20 17:55:57
|
A few weeks ago, I learned about reflection, R = V + N*(V‧N). It works with velocity, and so far, I have no problems with it. Now I am expanding it further, by applying it to an object with a constant acceleration hitting towards a Pinball Bumper. That's right, a bumper with the ability to knock objects away with a higher velocity. Assuming: - O is an object with a constant acceleration heading west,
- B is a circular bumper on the path of O, and it doesn't move,
- A(O) stands for the old acceleration vector of the object,
- V(O) stands for velocity vector of the object, and
- P(O) stands for position vector of the object, and finally,
- A'(O) stands for the new acceleration vector of the object.
My current draft is: normalize(N), where N is normal vector of (O hit B)'s point of collision. R = A(O) + N*(A(O)‧N) A'(O) = R - A(O) V(O) += A'(O) P(O) += V(O)The problem for me is the object, O, instantly warped away from Bumper and constantly stays moving in a westward direction, instead of (moving eastward, slowing down, changing directions, speeding up westward, and repeat the cycle). Does anyone know how to fix this? I hoped I have provided sufficient information. Thanks in advance.
|
|
|
|
|
2
|
Java Game APIs & Engines / Android / Re: Possible SoundPool broken method: load(String path, int priority)
|
on: 2012-09-22 05:19:44
|
All of these below are basically my hunch. I have still not confirmed that the method described works as intented. Again, please feel free to correct me and help out on this research. 
It's possible that the path in the method load(String path, int priority) stands for the path to an audio file located on the SD card. Am I correct? I do not know. Nor do I know anything that says the path is for the entire path directory to the audio file in the SD file, by the Documentation. It doesn't say, and probably nobody cares about it. I think it's confirmed that the "path" may or may not stands for the directory to the SD card on the Android phone. To fetch a more reliable directory (a starting point, like C:\> in cmd.exe of Windows variants), use helper methods, such as "Environment.getExternalStorageDirectory()". path may have different starting points in different devices, even if the devices in question is not a tablet, nor a phone. It could me /mnt/sda5, /mnt/card, etc. It is declared by the manufacturer what the Environment variables are. This is also a good way to learn more things while doing research. For now...
|
|
|
|
|
3
|
Java Game APIs & Engines / Android / Possible SoundPool broken method: load(String path, int priority)
|
on: 2012-09-14 11:59:37
|
I think this will never garner enough support by anyone who wishes to delve themselves deeper into such mystery. All of you probably knew by now, how unstable and how unreliable SoundPool class is. Some people manage to make it work, while others rage-quit themselves after tackling the problem many times. One of methods in SoundPool grabbed my attention, and it's this method: 1
| load(String path, int priority) |
Some sources in Stack Overflow suggests that method should not be used at all, and instead recommends other load() methods. There are no solutions to using the aforementioned code. This is a mystery. There is 1 source I found earlier (but can't find it again) that suggests the path parameter may stands for the path directory to an audio file located on the SD card, or in the external storage in some Android phones. I don't have the knowledge to prove it, so I can't tell. Here's my research, and wished others may help find a better solution to this: 1 2 3 4 5 6 7 8 9 10
| sounds.play(sounds.load("assets/test", 1), 1f, 1f, 1, 0, 1f); sounds.play(sounds.load("/assets/test", 1), 1f, 1f, 1, 0, 1f); sounds.play(sounds.load("assets/test.wav", 1), 1f, 1f, 1, 0, 1f); sounds.play(sounds.load("/assets/test.wav", 1), 1f, 1f, 1, 0, 1f);
|
Feel free to share your findings. EDIT: Just to clarify, I am able to successfully use SoundPool to play sounds. I do not have any problems with the following methods: 1 2 3 4
| load(Context context, int resId, int priority); load(FileDescriptor fd, long offset, long length, int priority); load(AssetFileDescriptor afd, int priority); |
The only load() method that is unable to work is the load(String path, int priority) method.
|
|
|
|
|
4
|
Game Development / Game Mechanics / Re: Bouncing Ball on the Z axis: I can't fix a math error without help. (GIF)
|
on: 2012-09-03 16:35:27
|
I was playing an NDS game, when suddenly an inspiration popped into my head after looking at a in-game statue sprite. After tinkering with the idea, I finally fixed the problem. Here's the code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| float radians; float radianSpeed; float multipler; float result;
public void calculate(){ result = (float) Math.cos(radians + Math.PI) * multipler + multipler; radians += 0.2f * radianSpeed; if (radians > 2 * Math.PI){ radianSpeed += 1; radians = 0f; multipler -= 1; if (multipler <= 0){ multipler = 0f; radians = 0f; } } }
|
|
|
|
|
|
5
|
Game Development / Game Mechanics / Bouncing Ball on the Z axis: I can't fix a math error without help. (GIF)
|
on: 2012-08-30 16:33:37
|
Here's a GIF depicting the way I want my ball to bounce.  And here's the code, the problem currently unsolved. "dstRect" is a custom class that allows me to blit a bitmap onto the Canvas: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| private void jump() { double value = Math.cos(radians + Math.PI) * multiplier; dstRect.left -= position[2] + oldXOffset; dstRect.right += position[2] + oldXOffset; dstRect.top -= position[2] + oldXOffset; dstRect.bottom += position[2] + oldXOffset; position[2] = (float) value + 8; radians += 0.1 * radianSpeed; if (radians > 2 * Math.PI) { multiplier -= 4; value = Math.cos(radians + Math.PI) * multiplier; oldXOffset += position[2] - (float) value + 8; radians = 0; radianSpeed += 2; if (multiplier < 2.0) { jumping = false; multiplier = 8; oldXOffset = 0f; radianSpeed = 1; position[2] = 0f; for (int i = 0; i < 2; i++) speed[i] = 0f; } } } |
The problem is this: It starts off by triggering a switch, where the ball itself goes into a jumping state. - On the first jump, it is normal. Ball starts jumping from the ground, flies into the air, and back down to the ground.
- On the second jump, it starts glitching out. Ball starts from the middle in the air, flies even higher, and back to the middle.
- On the third and last jump, the ball starts 1/3 from the peak of its jump, flies up to its peak, and back down 1/3 from its peak.
After that, the ball exits the jumping state, the ball goes back to the ground. I wished I could think more on this. 
|
|
|
|
|
6
|
Game Development / Game Mechanics / Re: Canvas: How do you make the Canvas as a chase camera for moving objects?
|
on: 2012-08-29 08:40:41
|
Very tricky to pull it off, but I managed to successfully implement a working prototype. Thanks!  Credit goes to loom_weaver. Code for Curious: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public void tick(RenderView view) { vx += Accelero.X * 0.1; vy += Accelero.Y * 0.1; x += vx; y += vy; view.translateX = x; view.translateY = y; vx *= 0.1; vy *= 0.1; }
public void render(Canvas c, float translateX, float translateY, final float centerWidth, final float centerHeight) { if (bitmap == null || c == null) return; float xOffset = x - translateX; float yOffset = y - translateY; move(centerWidth + xOffset, centerHeight + yOffset); c.drawBitmap(bitmap, srcRect, dstRect, null); } |
|
|
|
|
|
9
|
Game Development / Game Mechanics / Canvas: How do you make the Canvas as a chase camera for moving objects?
|
on: 2012-08-28 18:17:37
|
Here's Java-Gaming.org pastebin of my currently managed code.StoryTime!  I have successfully managed to create a simple chase camera. The camera itself is the Canvas. The region of the camera is the region of the Canvas. The camera (Canvas) follows a stationary unit, while other objects move in opposite directions, creating the illusion that the stationary unit is moving and the camera is "chasing" it. But, a friend of mine points that my simplistic design is a bad design if I were to expand on this. For example, if I were to add lots of objects to my game, I would have to calculate each and every single object. The friend thinks that the Canvas could just follow around the unit itself, while the unit moves around stationary obstacles, making the game more flexible and more easier to code. So I did a rewrite, albeit unsuccessful. It's like this diagram shown here:  The trouble I'm having is that, due to some technical difficulties, I can only obtain the center position, (x,y), of the Canvas. The map itself is loaded from a binary file, with everything from trees, to guns, to doors, etc. all fully created in the file. If I were to just load the file, my unit (the purple dot in the diagram) would spawn in at the top left corner. The main problem, thus the question, is how do I map the Canvas's center position onto the purple dot all the time? The hint I'm getting is that, if I can obtain the unit's position coordinates, then do something I have no clue on, I can then take the results from there, and offset all of the objects. But, wouldn't that defeat the purpose of what my friend is thinking of?  Thanks in advance.
|
|
|
|
|
10
|
Game Development / Game Mechanics / Re: Gravity hole: Objects moving near a hole, shifts its projection towards the hole
|
on: 2012-08-03 17:25:02
|
The last problem I noticed immediately is my inability to split the gravity push/pull force into X and Y vectors on the Cartesian coordinates. Line of pseudo-code process: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| final double G;
deltaX = a.position[0] - b.position[0]; deltaY = a.position[1] - b.position[1]; distance = Math.hypot(deltaX, deltaY); angle = Math.atan2(deltaX, deltaY);
force = G * a.mass * b.mass / (distance * distance);
x = force * Math.cos(angle); y = force * Math.sin(angle); |
God forbids me to get a good job. *sigh* Anyway, I'll check your source codes and will update my own version of integration tomorrow. 11:25PM, signing out.
|
|
|
|
|
11
|
Game Development / Game Mechanics / Re: Gravity hole: Objects moving near a hole, shifts its projection towards the hole
|
on: 2012-08-03 11:52:47
|
I noticed something as soon as I try the implementation. The balls will eventually go towards the hole, but very slowly, while it's bouncing around and around the hole. I tried tweaking it, so that if the ball gets closer and closer to the hole, the speed increases. I realized that if I were to hit the balls affected by the gravity pull with my cue ball, the balls ignore their current position and continues to move around, despite my cue balls changing their positions directly. This behavior isn't what I wanted, and they seemed to be off by many factors. I've been tweaking it until I fell asleep on the keyboard, so I must've been thinking too hard on this problem. Here's the current tweaked code: 1 2 3 4 5 6 7 8
| public void gravityPull(Hole h){ double dx = (this.position[0] - h.x); double dy = (this.position[1] - h.y); dx *= 0.00000001; dy *= 0.00000001; this.speed[0] += (float) 0.9 * dx - 1; this.speed[1] += (float) 0.9 * dy - 1; } |
EDIT: Actually, I'm adding buffers to this. If we look at this one-dimensionally, for example, on the X axis, we could see that the values tend to increase/decrease around the targeted X value (where the hole is located on the X axis). The values go up and down, as show here.  What I'm doing is to try to make it so that when the ball goes past the targeted X value, and slow down, just like the red graph shown above. I thought that if I give the Dx and Dy values a bit more intensity, it will work. But that is not the case.
|
|
|
|
|
13
|
Game Development / Game Mechanics / Re: Gravity hole: Objects moving near a hole, shifts its projection towards the hole
|
on: 2012-08-02 16:23:14
|
Note that it's not just any golf games. It could be any space themed games. By continuing this discussion further, I would like to try taking such a challenge and be able to get this mock Java code up on here: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public void suckIntoHole(Ball b, Hole h){ float ballRadius = b.diameter /2; float holeRadius = 16; float dx = b.position[0] - h.x + (ballRadius + holeRadius); float dy = b.position[1] - h.y + (ballRadius + holeRadius); double angle = Math.atan2(dx, dy); double lx = b.speed[0]*Math.cos(angle)-b.speed[1]*Math.sin(angle); double ly = b.speed[1]*Math.cos(angle)+b.speed[0]*Math.sin(angle); b.speed[0] = (float) lx; b.speed[1] = (float) ly; b.position[0] = (float)(lx*Math.cos(-angle)-ly*Math.sin(-angle)); b.position[1] = (float)(ly*Math.cos(-angle)+lx*Math.sin(-angle)); } |
My hypothesis is like this:  But in practice, the ball went awkward... And didn't quite bend around the hole like I expected.
|
|
|
|
|
17
|
Game Development / Game Mechanics / Gravity hole: Objects moving near a hole, shifts its projection towards the hole
|
on: 2012-07-31 12:09:50
|
I'm just curious. You know that golf holes in any games that "attracts, or sucks, balls into/towards the hole" game mechanic?  I find games in space, such as Angry Birds Space on Android produces some zones in which objects spins around the planet when interacting with gravity, intriguing with gravity involved. Like how black holes interact with objects, how gravity distorts the original velocity vector of an object towards the center, etc. I'm curious about: - What is the name for the "gravity hole" game mechanic? (Depicted as such in the picture.) I would like to know the most generic name, so I can find it easily on Google.
- How it was done exactly? Does one rotate the dot product of (the normalized vector from center of hole to point of collision) and (speed vector of any object within range of the gravity hole) towards the center of the hole?
|
|
|
|
|
19
|
Game Development / Game Mechanics / Re: Need help on calculating the reflection of a point hitting a circle from inside.
|
on: 2012-07-26 16:06:05
|
Sorry I didn't reply as soon as possible. It worked! I felt stupid the moment I see that it worked on first try. I had worked 2 long weeks trying out with angles, rotating, etc. (The codes you see before hand.) But, it felt good.  Thank you! Here's the resulting code, for future references: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| public void calculateResponse() { for (int i = 0; i <= 1; i++) { position[i] -= speed[i]; speed[i] *= 0.9992f; } } public void reflect(Hole h){ double vx = this.speed[0]; double vy = this.speed[1]; double nx = (this.position[0]+this.diameter/2) - (h.x+16); double ny = (this.position[1]+this.diameter/2) - (h.y+16); double nd = Math.hypot(nx, ny); if (nd == 0) nd = 1; nx /= nd; ny /= nd; double dotProduct = vx*nx+vy*ny; double rx = -2*dotProduct*nx+vx; double ry = -2*dotProduct*ny+vy; this.speed[0] = (float) rx; this.speed[1] = (float) ry; }
private void move() { if (position[0] > screenWidth - this.diameter) position[0] = screenWidth - this.diameter; if (position[1] > screenHeight - this.diameter) position[1] = screenHeight - this.diameter; for (int i = 0; i <= 1; i++) if (position[i] < 0) position[i] = 0; dstRect.set(position[0], position[1], position[0] + this.diameter, position[1] + this.diameter); }
public void tick(Level l) { if (bitmap != null) { if (!jumping) { if (l.goalReached) { reflect(l.hole); calculateResponse(); move(); } else { calculate(); move(); } } else jump(); if (acceleration[2] < 0.0) { multiplier = 10; jumping = true; } } else bitmap = Art.sprites; } |
|
|
|
|
|
20
|
Game Development / Game Mechanics / Re: Need help on calculating the reflection of a point hitting a circle from inside.
|
on: 2012-07-24 17:01:32
|
I'm happy to say that, after some work, I got a weird response. When the point is inside the circle, it will jitter quickly while moving along the edge of the circle, before moving out of the circle. It's not completely bounded and I don't know why. To be honest, I couldn't see how the code is having problems. Angles really do make calculations more difficult to maintain. Here's the code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public void bind(Hole h) { double x1 = 0, y1 = 0, x = 0, y = 0, cx = 0, cy = 0, angle = 0, newX= 0, newY= 0; double distance1 = Math.hypot((this.position[0]+(this.diameter)/2) - (h.x+16), (this.position[1]+(this.diameter)/2) - (h.y+16)); double distance2 = Math.hypot((this.position[0]-this.speed[0]+(this.diameter)/2) - (h.x+16), (this.position[1]-this.speed[1]+(this.diameter)/2) - (h.y+16)); DecimalFormat f = new DecimalFormat("'+'000.;'-'000."); DecimalFormat g = new DecimalFormat("'+'000.000;'-'000.000"); if (distance2 >= 16 || distance1 <= 12){ x = this.position[0]; y = this.position[1]; cx = h.x; cy = h.y; angle = Math.atan2(y - cy, x - cx); newX = x * Math.cos(angle) + y * Math.sin(angle); newY = y * Math.cos(angle) - x * Math.sin(angle); newX += this.speed[0]; newY -= this.speed[1]; x1 = newX * Math.cos(-angle) + newY * Math.sin(-angle); y1 = newY * Math.cos(-angle) - newX * Math.sin(-angle); this.position[0] = (float) x1; this.position[1] = (float) y1; } }
public void calculateResponse() { for (int i = 0; i <= 1; i++) { speed[i] += acceleration[i]; speed[i] *= 0.1f; } }
private void move() { if (position[0] > screenWidth - this.diameter) position[0] = screenWidth - this.diameter; if (position[1] > screenHeight - this.diameter) position[1] = screenHeight - this.diameter; for (int i = 0; i <= 1; i++) if (position[i] < 0) position[i] = 0; dstRect.set(position[0], position[1], position[0] + this.diameter, position[1] + this.diameter); } |
Here's a picture, showing how the ball reacts accordingly from left to right:  So, the jittering-ness and the random shootoffs are getting quite unusual for me. Anyone know an explanation for this?
|
|
|
|
|
22
|
Game Development / Newbie & Debugging Questions / To convert a positive number X into a negative number (-X) is called "negating"?
|
on: 2012-07-24 10:34:20
|
Negate [verb] (used without object) - To be negative; bring or cause negative. I'm looking for a programming term used to mean "a way to convert a positive number into its negative form." For example: 10. I "negate" that to -10. 23. After negation: -23. -15. Negate it. 15. etc. It's comparable to how I think I use the word NOT, or !, in daily usage. Example usage of NOT: Example usage of NOT in sentences: "See this flag? Use the NOT value of the flag." "Try NOTting the value and NOR it with the result." "NOT the XOR value from the base." Is the word "negate" optimal for the usage of such scenario? Thanks in advance.
|
|
|
|
|
23
|
Game Development / Game Mechanics / Re: Need help on calculating the reflection of a point hitting a circle from inside.
|
on: 2012-07-24 09:21:53
|
Just realized there's no Multi-Quote feature in this forum. So, copy/pasting... And another thing. I have this habit of pasting my codes in my post when unnecessary. So, please take your time. I don't think you may read the codes. Just that, everywhere I've been to, I've been asked for the codes. for reflection you only need the normal. And the normal for every point which lies on the circle is the direction vector to the center of the circle.
Apparently, I got stuck at this point even more than I expected. Once we have two points, and find the normal, how do I find out the direction of the normal it is facing? There are two normals to a vector; they have two directions, pointing oppositely (A real word, by the way). If I wish to take only one of the two normals, do I need to rely on the angle calculations based off of the Cartesian coordinates of the point of collision? Or I need to do lots of if...else... conditions? I've been trying so hard to find that link of yours. Sometimes, Google never work out as much as I wanted it to be. Thanks for the link. EDIT: Noticed that in the "Circular Container" section, it uses the (time and speed) to acquire the distance displacement, and then add the displacement to the current ball position. Here's my hunch. I can substitute the time for a game tick, and the X amount of ticks passed will give me the a constant time slice T/X (where T is in the boundaries of the nanoseconds), which I can use to acquire the distance displacement, and calculate() it to the ball position. So, I may rewrite the algorithm so that T=1 in the tick(), and that could help me shorten the amount of vector calculations. Is this plausible? I see the current algorithm in his section, probably requiring some more passes before the correct position is obtained.
|
|
|
|
|
24
|
Game Development / Game Mechanics / Need help on calculating the reflection of a point hitting a circle from inside.
|
on: 2012-07-23 11:06:21
|
Here's a simple diagram of the point and a circle the title is describing about.  As far as I know, when the point hits the circle from the inside, as shown above, the point of collision is registered via a simple collision detection. Something confuses me is the tangent of the collision point. When I obtain the point of collision's Cartesian coordinates, use the center of the circle to obtain the vector from it, I only know that I am able to calculate the Theta angle (from X axis to the vector) and Phi angle (from Y axis to the vector) of the collision point to the center of the circle by using Math.atan2(), which I think is not able to help me in finding the normal of the point is moving at. Here's how I calculate the ball's projectory: 1. Via some specific way of obtaining values from an accelerometer, I set the values and load them in a Java float array. These values are the acceleration values for X, Y and Z axes of the point in a 3D world. For now, we will focus on a 2D top-down view, with Z positive is up, Y positive is front, and X positive is to the right. 2. The acceleration values are then feed into the speed values. 3. Then I continuously add the speed values to the position values. In between these, I do not rely on a Time variable. I was wondering if anyone knows how I should calculate the actual normal vector of the point? Thanks in advance. Here's the source code for the point. Initializations: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class Cue extends Point { protected Bitmap bitmap = null; protected final Rect srcRect = new Rect(); protected RectF dstRect = new RectF(); private boolean jumping = false; private double radians = 0; private double radianSpeed = 1.0; private double multiplier = 0; private double oldXOffset = 0; private float[] jumpPositionSpeed = new float[3]; private Paint paint; private boolean goingOut = false; public Cue(int w, int h) { srcRect.set(0, 0, 16, 16); dstRect.set(0, 0, 16, 16); this.diameter = (srcRect.right - srcRect.left); for (int i = 0; i < 2; i++) jumpPositionSpeed[i] = 0f; jumping = false; this.setBoundary(w, h); paint = new Paint(); paint.setARGB(255, 255, 255, 255); goingOut = false; } |
Gameticks and rendering: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public void render(Canvas c) { if (bitmap != null) { c.drawBitmap(bitmap, srcRect, dstRect, paint); } } public void tick(Level l) { if (bitmap != null) { if (!jumping) { if (l.goalReached){ bind(l.hole); } calculate(); move(); } else jump(); if (acceleration[2] < 0.0) { multiplier = 10; jumping = true; } } else bitmap = Art.sprites; } |
Bind function: Used for calculating how I should keep the point inside the boundaries I set forth. Ignore the //FIXME comment. I placed it there to remind me in third person, so my PC is like a boss. 1 2 3 4 5 6 7 8
| public void bind(Hole h){ double distance = Math.hypot(this.position[0] - h.x + (16+this.diameter)/2, this.position[1] - h.y + (16+this.diameter)/2); if (distance > (16+this.diameter)/2){ } } |
Actions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| private void jump() { int xOffset = (int) (Math.cos(radians + Math.PI) * multiplier) + 8; for (int i = 0; i < 2; i++) { jumpPositionSpeed[i] += acceleration[i]; position[i] -= jumpPositionSpeed[i]; jumpPositionSpeed[i] *= 0.1f; } if (position[0] > screenWidth - this.diameter) position[0] = screenWidth - this.diameter; if (position[1] > screenHeight - this.diameter) position[1] = screenHeight - this.diameter; if (position[0] < 0) position[0] = 0; if (position[1] < 0) position[1] = 0; dstRect.set(position[0], position[1], position[0] + this.diameter, position[1] + this.diameter); dstRect.left -= xOffset + oldXOffset; dstRect.right += xOffset + oldXOffset; dstRect.top -= xOffset + oldXOffset; dstRect.bottom += xOffset + oldXOffset; radians += 0.1 * radianSpeed; if (radians > 2 * Math.PI) { multiplier -= 4; oldXOffset += xOffset - (int) ((Math.cos(radians + Math.PI) * multiplier) + 8); radians = 0; radianSpeed += 2; if (multiplier < 2.0) { jumping = false; multiplier = 10; oldXOffset = 0; radianSpeed = 1.0; for (int i = 0; i < 2; i++) jumpPositionSpeed[i] = 0; } } } private void move() { if (position[0] > screenWidth - this.diameter) position[0] = screenWidth - this.diameter; if (position[1] > screenHeight - this.diameter) position[1] = screenHeight - this.diameter; for (int i = 0; i<= 1; i++) if (position[i] < 0) position[i] = 0; dstRect.set(position[0], position[1], position[0] + this.diameter, position[1] + this.diameter); } |
This function is made to calculate the values from the accelerometer to the screen. 1 2 3 4 5 6 7 8
| public void calculate(){ for (int i = 0; i < 2; i++) { speed[i] += acceleration[i]; position[i] -= speed[i]; speed[i] *= 0.1f; } } |
Misc. functions 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public boolean isAlive() { return true; } public boolean isJumping(){ return jumping; } public void stopJumping() { jumping = false; multiplier = 10; oldXOffset = 0; radians = 0; radianSpeed = 1.0; for (int i = 0; i < 2; i++) jumpPositionSpeed[i] = 0; } public void reset(int x, int y){ position[0] = x - 8; position[1] = y - 8; speed[0] = 0f; speed[1] = 0f; stopJumping(); } |
Collision helper functions for other collision detections/responses 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public void resolveCollision(Hole h){ float distance = (16 + this.diameter) / 2; float dx = this.position[0] - h.x + distance; float dy = this.position[1] - h.y + distance; double dist = Math.hypot(dx, dy); double penetration = Math.max(0, (distance - dist)); if (distance > dist) { this.position[0] += ((float) (penetration * dx)) / (float) (dist * 2); this.position[1] += ((float) (penetration * dy)) / (float) (dist * 2); } } public void collisionResponse(Hole h){ double xVelocity = this.speed[0]; double yVelocity = this.speed[1]; double xDist = this.position[0] - h.x + (16 + this.diameter)/2; double yDist = this.position[1] - h.y + (16+this.diameter)/2; double distSquared = xDist * xDist + yDist * yDist; double dotProduct = xDist * xVelocity + yDist * yVelocity; if (dotProduct > 0) { double collisionScale = dotProduct / distSquared; double xCollision = xDist * collisionScale; double yCollision = yDist * collisionScale; this.speed[0] -= xCollision; this.speed[1] -= yCollision; } } public void calculateResponse(){ for (int i = 0; i<=1; i++){ position[i] -= speed[i]; } } } |
The code is probably one of the most ugliest ones you seen here, so I apologize. Sorry.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|